A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
  1. import java.util.Scanner;
  2. public class Twenty_fifthPalindrom {
  3.         static int[] a = new int[5];
  4.         static int[] b = new int[5];
  5.         public static void main(String[] args) {

  6.                 boolean is =false;
  7.                 Scanner s = new Scanner(System.in);
  8.                 long l = s.nextLong();

  9.                 if (l > 99999 || l < 10000) {
  10.                         System.out.println("Input error, please input again!");
  11.                         l = s.nextLong();
  12.                 }

  13.                 for (int i = 4; i >= 0; i--) {
  14.                         a[i] = (int) (l / (long) Math.pow(10, i));
  15.                         l =(l % ( long) Math.pow(10, i));
  16.                 }
  17.                 System.out.println();
  18.                 for(int i=0,j=0; i<5; i++, j++) {
  19.                         b[j] = a[i];
  20.                 }


  21.                 for(int i=0,j=4; i<5; i++, j--) {
  22.                         if(a[i] != b[j]) {
  23.                                 is = false;
  24.                                 break;
  25.                         } else {
  26.                                 is = true;
  27.                         }
  28.                 }
  29.                 if(is == false) {
  30.                         System.out.println("is not a Palindrom!");
  31.                 } else if(is == true) {
  32.                         System.out.println("is a Palindrom!");
  33.                 }
  34.         }
  35. }
复制代码
答案不是唯一的,有更好的解决方案请指教

1 个回复

倒序浏览
本帖最后由 M_J 于 2016-7-19 01:09 编辑
  1. package youyisi;
  2. import java.util.Scanner;
  3. //判断任意5位数是否为回文数
  4. public class HuiWen2 {
  5.         public static void main(String[] args) {
  6.                 Scanner sc = new Scanner(System.in);
  7.                 System.out.println("请输入一个5位整数");
  8.                 int m = sc.nextInt();
  9.                 if(m<10000 || m>99999){
  10.                         System.out.println("您输入的数不是5位整数,请重新输入!");
  11.                 }
  12.                 String sm = Integer.toString(m);
  13.                 String ss = "";
  14.                 for(int i=sm.length()-1;i>=0;i--){
  15.                         ss +=sm.charAt(i);
  16.                         if(sm.equals(ss)){
  17.                                 System.out.println(m+"是回文数");
  18.                         }
  19.                 }
  20.         }
  21. }
复制代码

回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马