找出一个数,各位数字的阶乘相加之和等于该数

 

 

找出一个数,各位数字的阶乘相加之和等于该数

package kgc.day06;

import java.util.Arrays;

/**
 * @Description:
 * @author:myh
 * @date: 2021/5/18  23:27
 */
public class jiecheng {
    public static void main(String[] args) {
        int a[]=new int[10];
//        System.out.println(weishu(0));
        Arrays.fill(a,-1);
//        System.out.println(Arrays.toString(a));
        int sum;
        // 21 123 1234
//        for(int i=1;i<=99999;i++){
        for(int i=1;i<=99999999;i++){
            sum=0;

            for (int i1 = 0; i1 < weishu(i); i1++) {
                if(i1>0&&i1<weishu(i)-1){
                    a[i1]=i/(int)Math.pow(10,i1)-i/(int)Math.pow(10,i1+1)*10;
                }
                else if(i1==0){
                    a[i1]=i%10;
                }else if(i1==weishu(i)-1){
                    a[i1]=i/(int)(Math.pow(10,i1));
                }
            }

//                if(weishu(i)==1){
//                    a[0]=i;
//                }else if(weishu(i)==2){
//                    a[0]=i%10;
//                    a[1]=i/10;
//                }else if(weishu(i)==3){
//                    a[0]=i%10;
//                    a[1]=i/10-i/100*10;
//                    a[2]=i/100;
//                }else if(weishu(i)==4){
//                    a[0]=i%10;
//                    a[1]=i/10-i/100*10;
//                    a[2]=i/100-i/1000*10;
//                    a[3]=i/1000;
//                }else if(weishu(i)==5){
//                    a[0]=i%10;          //12345
//                    a[1]=i/10-i/100*10;
//                    a[2]=i/100-i/1000*10;   //40585
//                    a[3]=i/1000-i/10000*10;
//                    a[4]=i/10000;
//                }
            for (int i1 = 0; i1 < a.length; i1++) {
                if(a[i1]!=-1){
                    sum+=jiecheng(a[i1]);
                }
            }


            if(i==sum){
                System.out.println(i);
            }
        }

    }
    public static int jiecheng(int n){
        int m=1;

        if(n==0){
            return 1;
        }else{
            for (int i = 1; i <= n; i++) {
                m*=i;
            }
            return m;
        }

    }
    public static int weishu(int m){
        int t=0;
        do{
            m=m/10;
            t++;
        }while(m!=0);
        return t;
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值