/**
问题描述
123321是一个非常特殊的数,它从左边读和从右边读是一样的。
输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
输入格式
输入一行,包含一个正整数n。
输出格式
按从小到大的顺序输出满足条件的整数,每个整数占一行。
样例输入
52
样例输出
899998
989989
998899
思路:
因为这里涉及到5位数和6位数,不能用同一套切分数字公式,所以分两个循环来讨论
package 蓝桥基础练习;
import java.util.Scanner;
public class BASIC_9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 10000; i < 1000000; i++) {// 12345
int five = i / 10000;
int four = i / 1000 % 10;
int three = i / 100 % 10;
int two = i / 10 % 10;
int one = i % 10;
if (one == five && two == four) {
if ((five + four + three + two + one) == n)
System.out.println(i);
}
}
for (int i = 100000; i < 10000000; i++) {// 123456
int six = i / 100000;
int five = i / 10000 % 10;
int four = i / 1000 % 10;
int three = i / 100 % 10;
int two = i / 10 % 10;
int one = i % 10;
if (one == six && two == five && three == four) {
if ((six + five + four + three + two + one) == n)
System.out.println(i);
}
}
}
}
网友评论