特殊回文数

问题描述
  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值