ZZULIOJ-1107: palindrome conjecture (function topic) (Java)

Subject description:

A positive integer, if read from left to right (sequence number referred to as positive) and read from right to left (reverse call number) is the same, such a number is called palindrome. Either take a positive integer, if not a palindrome number, the number is added to the number of his reverse, and if it is not a palindrome number, repeat above steps until palindrome obtained so far. For example: 154 becomes 68 (68 + 86), then becomes 605 (154 + 451), and finally becomes 1111 (605 + 506), and 1111 is a palindrome. So mathematicians have proposed a guess: no matter what began as a positive integer, after a number of positive steps in reverse order finite number of times and added, you will get a palindrome. So far I do not know this conjecture is right or wrong. Now you programmed verification purposes. You will have to write if functional inverse of an integer number of reverse (), then the following cycle may simulate palindrome conjecture verification process:
the while (! M = inverse (n), n = m)
{
    output n;
    the n Update of + n-m;

Input: 

Enter a positive integer. Special note: input data to ensure intermediate result is less than 2 ^ 31.  

Output: 

Output per line, obtained in the process of transformation of values ​​between the two numbers separated by a space.  

Sample input: 

27228 

Sample output: 

27228 109500 115401 219912 

code: 

import java.util.*;
public class Main
{
	public static int inverse(int n)
	{
		int sum=0;
		while(n!=0)
		{
			sum=sum*10+n%10;
			n/=10;
		}
		return sum;
	}
	public static void main(String[] args)
	{
		Scanner input=new Scanner(System.in);
		int n=input.nextInt();
		int m=Main.inverse(n);//先让m等于n的逆序数
		System.out.print(n);//先将n输出,第一个数的后面不加空格
		while(m!=n)//按照题意m不等于n
		{
			n=m+n;//将n和其逆序数相加
			m=Main.inverse(n);//继续求其逆序数
			System.out.print(" "+n);//之后的每个数前面都要加上空格,避免最后一个数的后面没有空格
		}
		input.close();
	}
}

 

Published 260 original articles · won praise 267 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43823808/article/details/103756905