<MEMORY>Project Euler NO38

来源:互联网 发布:编程数学 编辑:程序博客网 时间:2024/04/30 14:26
将192与1,2,3分别相乘得到:
192 × 1 = 192
192 × 2 = 384
192 × 3 = 576
将这三个乘积连接起来我们得到一个1到9的pandigital数, 192384576。我们称 192384576是192和 (1,2,3)的连接积。
通过将9与1,2,3,4和5相乘也可以得到pandigital数:918273645,这个数是9和(1,2,3,4,5)的连接积。

用一个整数与1,2, ... , n(n大于1)的连接积构造而成的1到9pandigital数中最大的是多少?



public class Problem38{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(){int max = 0;for (int i = 1; i < 99999; i++){String str = "";for (int j = 1; str.length() < 9; j++){str += i * j + "";}if (str.length() == 9 && check(str)){int t = Integer.parseInt(str);if (max < t){max = t;}}}System.out.println(max);}static boolean check(String str){char ch[] = str.toCharArray();for (int i = 0; i < ch.length; i++){if (ch[i] == '0'){return false;}for (int j = i + 1; j < ch.length; j++){if (ch[j] == '0'){return false;}if (ch[i] == ch[j]){return false;}}}return true;}}



answer:  932718654
time:  94

0 0