转载请注明出处:http://www.cnblogs.com/xiaovw/articles/8353793.html
题目描述:
解题思路:
依次枚举100000-1000000之间的数 i ,
1、转换为字符串strpre,把原字符串strpre与反转后的字符串strturn进行比较,如果相同则判定该字符串是回文数。
2、把数值i付值给number,用辗转相除法取出number的每一位累加到sum上,如果sum和输入数值n相同,则数值i即为符合题意的特殊回文数。
锦囊妙计:
代码展示:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int number,len,sum = 0,i,j; char[] array = new char[6]; String strpre = ""; String strturn = ""; StringBuilder strbuilder = new StringBuilder(); for(i = 10000; i<1000000; i++) { number = i; strpre = Integer.toString(i); array = strpre.toCharArray(); while(number > 0) { sum += number % 10; number = number / 10; } len = array.length; for(j = len - 1; j >= 0; j--) { strbuilder.append(array[j]); } strturn = strbuilder.toString(); if(strpre.equals(strturn)) { if(n == sum) { System.out.println(i); } } strbuilder = new StringBuilder(); sum = 0; } } }
评测状态:
小V