特殊回文数

问题描述
  123321是一个非常特殊的数,它从左边读和从右边读是一样的。
  输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
输入格式
  输入一行,包含一个正整数n。
输出格式
  按从小到大的顺序输出满足条件的整数,每个整数占一行。
样例输入
52
样例输出
899998
989989
998899
数据规模和约定
1<=n<=54。

import java.util.Scanner;
public class Main {  
    public static void main(String[] args)  
    {  
        Scanner sc =new Scanner(System.in);
        int count = sc.nextInt();
        for(int i=10000;i<100000 ;i++) {
            if(huiwen( i , 5, count)) {
                System.out.println(i);
            }
        }
        for(int j=100000;j<1000000 ;j++) {
            if(huiwen( j , 6, count)) {
                System.out.println(j);
            }
        }
    } 
   public static boolean huiwen(int num ,int len,int count) {
       String str =String.valueOf(num);
       if(len==5) {
           if(str.charAt(0)!= str.charAt(4)||str.charAt(1)!= str.charAt(3)) {
               return false;
           }
       }else if(str.charAt(0)!= str.charAt(5)||str.charAt(1)!= str.charAt(4)||str.charAt(2)!= str.charAt(3)){
        return false ;
    }
       if(countnum( num, len , count)) {
           return true ;
       }else {
           return false;
    }

   }
   public static boolean countnum(int num,int len ,int count) {
       int cou =0 ;
       if(len==5) {
           cou+=(num/10000) ;
           cou+=((num%10000)/1000) ;
           cou+=((num%1000)/100) ;
           cou+=((num%100)/10) ;
           cou+=(num%10) ;
       }else {
           cou+=(num/100000) ;
           cou+=((num%100000)/10000) ;
           cou+=((num%10000)/1000) ;
           cou+=((num%1000)/100) ;
           cou+=((num%100)/10) ;
           cou+=(num%10) ;
       }
       if(cou==count){
           return true ;
       }
       return false ;
   }
} 

结果
输入:
25
输出:
17971
18781
19591
26962
27772
28582
29392
35953
36763
37573
38383
39193
44944
45754
46564
47374
48184
53935
54745
55555
56365
57175
62926
63736
64546
65356
66166
71917
72727
73537
74347
75157
80908
81718
82528
83338
84148
90709
91519
92329
93139

猜你喜欢

转载自blog.csdn.net/cqx13763055264/article/details/79416298