蓝桥杯试题 基础练习 BASIC-9 特殊回文数——冲刺蓝桥杯第五天
程序员文章站
2024-02-21 14:19:28
...
import java.util.ArrayList;
import java.util.Scanner;
public class SpecialPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
ArrayList<Integer> arrayList5 = new ArrayList<>(); //存放五位十进制数
ArrayList<Integer> arrayList6 = new ArrayList<>(); //存放六位十进制数
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
for (int p = 0; p < 10; p++) {
for (int m = 0; m < 10; m++) {
int a = i * 10000 + j * 1000 + k * 100 + p * 10 + m;
int b = m * 10000 + p * 1000 + k * 100 + j * 10 + i;
int c = i + j + k + p + m;
if (a == b && c == n && a >= 10000) {
arrayList5.add(a);
}
for (int l = 0; l < 10; l++) {
a = i * 100000 + j * 10000 + k * 1000 + p * 100 + m * 10 + l;
b = l * 100000 + m * 10000 + p * 1000 + k * 100 + j * 10 + i;
c = i + j + k + p + m + l;
if (a == b && c == n&&a >= 100000) {
arrayList6.add(a);
}
}
}
}
}
}
}
for (int i : arrayList5) {
System.out.println(i);
}
for (int i : arrayList6) {
System.out.println(i);
}
}
}