日期:2014-05-20 浏览次数:20917 次
public static long caculateHuiWenShu(int input)
{
if(input<0)
{
return (long)0;
}
StringBuffer sb=new StringBuffer();
sb.append(input);
long result=0;
for(long i=0;i<30;i++){
result=(Long.valueOf(sb.toString())+Long.valueOf(sb.reverse().toString()));
sb.setLength(0);
sb.append(result);
if(result==Long.valueOf(sb.reverse().toString())){
return (long)(i+1);
}
sb.setLength(0);
sb.append(result);
}
return (long)0;
}
public class Test1 {
public static void main(String[] args) {
long value = calcPlalindrome(78);
System.out.println(value);
}
static int calcPlalindrome(int num) {
final int err = 0;// 异常返回值
final int MAX_STEP = 30;// 最大步数
if (num < 0
------解决方案--------------------
isPlalindrome(num)) {
return err;
}
for (int i = 1; i <= MAX_STEP; ++i) {
int reverseNum = getReverseNum(num);
System.out.printf("STEP%d: %d+%d=%d\n", i, num, reverseNum, num
+ reverseNum);
num += reverseNum;
if (isPlalindrome(num))
return i;
}
return 0;
}
//把一个数的各位数字上数字倒过来排列组成一个新数
static int getReverseNum(int num) {
int newNum = 0;
while (num > 0) {
int n = num % 10;
newNum *= 10;