解题思路:
用StringBuffer的reverse()方法判断回文数就比较简单了
注意事项:
参考代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); boolean flag = false; for (int i = 10000; i <= 999999; i ++) { String str = String.valueOf(i); StringBuffer sb = new StringBuffer(str); String str1 = new String(sb.reverse()); if (str.equals(str1)) { char[] c = str.toCharArray(); int sum = 0; for (int j = 0; j < c.length; j++) { String s = String.valueOf(c[j]); int t = Integer.parseInt(s); sum += t; } if (sum == n) { flag = true; System.out.println(str1); } } } if (!flag) { System.out.println("-1"); } } }
0.0分
0 人评分