java算法:判断一个五位数是不是回文数
题目:一个 5 位数,判断它是不是回文数。即 12321 是回文数,个位与万位相同,十位与 千位相同。
package com.qq.weixin;
import java.util.Scanner;
public class Ex25 {
static int[] a = new int[5];
static int[] b = new int[5];
public static void main(String[] args) {
boolean is = false;
Scanner s = new Scanner(System.in);
long l = s.nextLong();
if (l > 99999 || l < 10000) {
System.out.println("Input error, please input again!");
l = s.nextLong();
}
for (int i = 4; i >= 0; i--) {
a[i] = (int) (l / (long) Math.pow(10, i));
l = (l % (long) Math.pow(10, i));
}
System.out.println();
for (int i = 0, j = 0; i < 5; i++, j++) {
b[j] = a[i];
}
for (int i = 0, j = 4; i < 5; i++, j--) {
if (a[i] != b[j]) {
is = false;
break;
} else {
is = true;
}
}
if (is == false) {
System.out.println("is not a Palindrom!");
} else if (is == true) {
System.out.println("is a Palindrom!");
}
}
}
还没有评论,来说两句吧...