7-4 sdut-JAVA-Palindromic Number
分数 16
全屏浏览
切换布局
作者 马新娟
单位 山东理工大学
"A palindromic number or numeral palindrome is a number that remains the same when its digits
are reversed." Examples of palindromic numbers are 1234321, 777, 1 and 66. You are required to write a Java application program that determines whether or not a number supplied by the end user is
palindromic.
Input Specification:
Input a integer number.
Output Specification:
display the result whether or not a number supplied by the end user is
palindromic.
Sample Input:
12321
Sample Output:
Number input:12321 Number in reverse:12321 Number is palindromic
Sample Input:
12334
Sample Output:
Number input:12334 Number in reverse:43321 Number is not palindromic
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
import java.util.Scanner; public class Main{ public static void main(String [] args){ Scanner in=new Scanner(System.in); int n=in.nextInt(); int m=0; int i=0,j=0; int x=n; for(i=0;x>0;i++){ x/=10; } for(j=0,x=n;j<i;j++){ m+=x%10*(Math.pow(10,i-j-1)); x/=10; } System.out.println("Number input:"+n); System.out.println("Number in reverse:"+m); if(m==n) System.out.print("Number is palindromic"); else System.out.print("Number is not palindromic"); }