<MEMORY>Project Euler NO56

来源:互联网 发布:python while循环列表 编辑:程序博客网 时间:2024/05/29 15:30

一个googol (10100)是一个巨大的数字:1后面跟着100个0;100100几乎是不可想象的大:1后面跟着200个0。它们虽然很大,但是它们的各位数之和却只有1。

考虑形如 ab 的数, 其中 a, b < 100,最大的各位和是多少?


import java.math.BigInteger;public class Problem56{public static void main(String[] args){long start = System.currentTimeMillis();System.out.print("answer:  ");howmany();long end = System.currentTimeMillis();System.out.print("time:  ");System.out.println(end - start);}static void howmany(){BigInteger t = BigInteger.ZERO;int max = 0, temp = 0;for(int a = 1; a < 100; a++){for (int b = 1; b < 100; b++){t = BigInteger.valueOf(a).pow(b);temp = add(t.toString());if (temp > max){max = temp;}}}System.out.println(max);}static int add(String str){char ch[] = str.toCharArray();int sum = 0;for (int i = 0; i < ch.length; i++){sum += (int)ch[i] - 48;}return sum;}}




answer:  972
time:  338



0 0
原创粉丝点击