• 欧拉工程第56题:Powerful digit sum


    题目链接

    Java程序

    package projecteuler51to60;
    
    import java.math.BigInteger;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeSet;
    
    class level56{    
        void solve0(){
            int maxsum=0;
            for(int a=2;a<100;a++){
                for(int b=2;b<100;b++){
                    BigInteger bigNum = BigInteger.valueOf(a).pow(b);
                    maxsum=Math.max(digitSum(bigNum), maxsum);
    //                System.out.println(maxsum);
                }
            }
            System.out.println(maxsum);
        }
        private  int digitSum(BigInteger n) {
            int sum = 0;
            String s = n.toString();
            for (int i = 0; i < s.length(); i++)
                sum += s.charAt(i) - '0';
            return sum;
        }
        int sumDigits(BigInteger num){
            int sum=0;
            BigInteger ten = BigInteger.valueOf(10);
            while(num.equals(0)){
                sum+=(num.mod(ten)).intValue();
                num = num.divide(ten);
            }
            return sum;
        }
    
        
    }
    public class Problem56 {
    
    
        public static void main(String[] args){
            long begin= System.currentTimeMillis();
            new level56().solve0();
            long end = System.currentTimeMillis();
            long Time = end - begin;
            System.out.println("Time:"+Time/1000+"s"+Time%1000+"ms");
        }
    
    }
    由于上面程序写的时间有点长了,不做过多分析,只贴程序
  • 相关阅读:
    Vue2 组件注册
    Vue2 CSS 过渡
    Vue2 过滤器
    Vue2 路由
    网页一次滚动一屏幕效果
    JavaScript作用域-声明提升(个人总结)
    JS函数作用域提升
    如何以计算机的方式去思考
    常用Git命令总结
    关于RBAC(Role-Base Access Control)的理解(转)
  • 原文地址:https://www.cnblogs.com/bbbblog/p/4716358.html
Copyright © 2020-2023  润新知