
目录
1 问题描述
问题描述
123321是一个非常特殊的数,它从左边读和从右边读是一样的。
输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
输入格式
输入一行,包含一个正整数n。
输出格式
按从小到大的顺序输出满足条件的整数,每个整数占一行。
样例输入
52
样例输出
899998
989989
998899
989989
998899
数据规模和约定
1<=n<=54。
2 解决方案
2.1 80分解法
初步看题,自己把题意给理解的太深奥了,对于输出的五位数和六位数,我以为00100也是符合要求的五位回文数,那么问题就来了,如何输出00100呢?其解法就只能使用字符串了,那么使用字符串后,问题又来了,如何对字符串进行从小到大排序呢?好吧,在这里又得把字符串转换成整数比较大小,计算排序位置,计算完后,再次输出字符串的顺序。说到这里,我个人感觉自己写的代码虽然复杂,但是计算的结果绝对比标答结果全,但是在蓝桥杯练习系统中判分却只有80分,郁闷,不过这次得吸取理解题意的教训了。
具体代码如下:
package com.liuzhen.systemExe; import java.util.HashSet;
import java.util.Scanner;
import java.util.Set; public class Main{ public void getReverseN(int n){
Set<String> set = new HashSet<String>();
for(int i = 0;i < 10;i++){
int x1 = i;
for(int j = 0;j < 10;j++){
int x2 = j;
for(int k = 0;k < 10;k++){
int x3 = k;
if(2*(x1+x2)+x3 == n){
char[] temp = new char[5];
temp[0] = (char) (x1 + 48);
temp[1] = (char) (x2 + 48);
temp[2] = (char) (x3 + 48);
temp[3] = (char) (x2 + 48);
temp[4] = (char) (x1 + 48);
String tempS1 = String.valueOf(temp);
set.add(tempS1); temp[0] = (char) (x2 + 48);
temp[1] = (char) (x1 + 48);
temp[2] = (char) (x3 + 48);
temp[3] = (char) (x1 + 48);
temp[4] = (char) (x2 + 48);
String tempS2 = String.valueOf(temp);
set.add(tempS2);
}
if(2*(x1+x2+x3) == n){
char[] temp = new char[6];
temp[0] = (char) (x1 + 48);
temp[1] = (char) (x2 + 48);
temp[2] = (char) (x3 + 48);
temp[3] = (char) (x3 + 48);
temp[4] = (char) (x2 + 48);
temp[5] = (char) (x1 + 48);
String tempS1 = String.valueOf(temp);
set.add(tempS1); temp[0] = (char) (x1 + 48);
temp[1] = (char) (x3 + 48);
temp[2] = (char) (x2 + 48);
temp[3] = (char) (x2 + 48);
temp[4] = (char) (x3 + 48);
temp[5] = (char) (x1 + 48);
String tempS2 = String.valueOf(temp);
set.add(tempS2); temp[0] = (char) (x2 + 48);
temp[1] = (char) (x1 + 48);
temp[2] = (char) (x3 + 48);
temp[3] = (char) (x3 + 48);
temp[4] = (char) (x1 + 48);
temp[5] = (char) (x2 + 48);
String tempS3 = String.valueOf(temp);
set.add(tempS3); temp[0] = (char) (x2 + 48);
temp[1] = (char) (x3 + 48);
temp[2] = (char) (x1 + 48);
temp[3] = (char) (x1 + 48);
temp[4] = (char) (x3 + 48);
temp[5] = (char) (x2 + 48);
String tempS4 = String.valueOf(temp);
set.add(tempS4); temp[0] = (char) (x3 + 48);
temp[1] = (char) (x2 + 48);
temp[2] = (char) (x1 + 48);
temp[3] = (char) (x1 + 48);
temp[4] = (char) (x2 + 48);
temp[5] = (char) (x3 + 48);
String tempS5 = String.valueOf(temp);
set.add(tempS5); temp[0] = (char) (x3 + 48);
temp[1] = (char) (x1 + 48);
temp[2] = (char) (x2 + 48);
temp[3] = (char) (x2 + 48);
temp[4] = (char) (x1 + 48);
temp[5] = (char) (x3 + 48);
String tempS6 = String.valueOf(temp);
set.add(tempS6);
}
}
}
}
int len = set.size();
String[] result = new String[len];
int i = 0;
for(String tem : set)
result[i++] = tem;
int[] rI = getSort(result);
for(i = 0;i < len;i++){
int k = getMinI(rI);
System.out.println(result[k]);
rI[k] = len;
}
} public int[] getSort(String[] A){
int len = A.length;
int[] result = new int[len];
int[] tempResult = new int[len];
for(int i = 0;i < len;i++){
tempResult[i] = Integer.parseInt(A[i]);
}
for(int i = 0;i < len;i++){
int count = 0;
for(int j = 0;j < len;j++){
if(tempResult[i] > tempResult[j])
count++;
}
result[i] = count;
} return result;
} public int getMinI(int[] A){
int result = 0;
for(int i = 0;i < A.length;i++){
if(A[result] > A[i])
result = i;
}
return result;
} public static void main(String[] args){
//long t1 = System.currentTimeMillis();
Main test = new Main();
Scanner in = new Scanner(System.in);
System.out.println("请输入一个10进制整数:");
int n = in.nextInt();
test.getReverseN(n);
//long t2 = System.currentTimeMillis();
//System.out.println("耗时:"+(t2-t1)+"毫秒"); }
}
运行结果:
请输入一个10进制整数:
1
00100 请输入一个10进制整数:
2
00200
01010
001100
10001
010010
100001 请输入一个10进制整数:
52
899998
989989
998899
2.2 网友标答解法
具体代码如下:
package com.liuzhen.array_2; import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入数字n:");
int n=input.nextInt();
ArrayList<Integer> rs= new ArrayList<Integer>(); for(int i=1; i<10; i++)
for(int j=0; j<10; j++)
for(int k=0; k<10; k++){
if(2*i+2*j+k==n)
rs.add(i*10000 + j*1000 + k*100+ j*10 + i);
if(2*i+2*j+2*k==n)
rs.add(i*100000 + j*10000+ k*1000 + k*100+ j*10 + i);
}
Collections.sort(rs);
for(int i=0; i< rs.size(); i++)
System.out.println(rs.get(i));
} }
运行结果:
请输入数字n:
1 请输入数字n:
2
10001
100001 请输入数字n:
52
899998
989989
998899