解题思路:
5位或6位的截取一般都是3位数: 循环次数为 (29)*2 = 58
分别查找5位或6位满足的3位数,找到则计数器加1; 最后判断计数器若为0,则输出-1.
注意事项:
参考代码:
import java.util.Scanner; public class C1434 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { F(sc.nextInt()); } sc.close(); } private static void F(int n){ int count = 0; //5位或6位的截取一般都是3位数: 循环次数为 (29)*2 = 58 //寻找5位的 for(int a = 1; a <= 9; a++){ for(int b = 0; b <= 9; b++){ for(int c = 0; c <= 9; c++){ if(2*a + 2*b + c == n){ System.out.println("" + a + b + c + b + a); count++; } } } } //寻找6位的 for(int a = 1; a <= 9; a++){ for(int b = 0; b <= 9; b++){ for(int c = 0; c <= 9; c++){ if(2*(a+b+c) == n){ System.out.println("" + a + b + c + c + b + a); count++; } } } } if(count == 0) System.out.println(-1); } }
0.0分
10 人评分