<MEMORY>Project Euler NO43

来源:互联网 发布:有java基础学安卓 编辑:程序博客网 时间:2024/06/07 10:39

1406357289是一个pandigital数,因为它包含了0到9之间每个数字且只包含了一次。此外它还有一个有趣的子串整除性质。

d1表示其第一位数字,d2表示第二位,以此类推。这样我们可以得到:
  • d2d3d4=406 能被 2 整除
  • d3d4d5=063 能被 3 整除
  • d4d5d6=635 能被 5 整除
  • d5d6d7=357 能被 7 整除
  • d6d7d8=572 能被 11 整除
  • d7d8d9=728 能被 13 整除
  • d8d9d10=289 能被 17 整除

求所有具有如上性质的0到9pandigital数。


import java.util.Arrays;public class Problem43{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 int array[] = new int[10];static void howmany(){int qiuyu[] = {2,3,5,7,11,13,17};for (int i = 100; i <= 999; i++){array[0] = i / 100;array[1] = i / 10 % 10;array[2] = i % 10;zuhe(i, qiuyu, 0);}System.out.println(answer);}static long answer = 0;static void zuhe(int n, int qiuyu[], int k){if (k == 7){if (ispandi()){long s = 0;for (int i = 0; i < 10; i++){s = s * 10 + array[i];}answer += s;}return;}for (int i = 0; i <= 9; i++){int temp = (n % 100 * 10 + i);if ( temp  % qiuyu[k] == 0  ){array[k + 3] = temp % 10;zuhe(temp, qiuyu, k + 1);}}}static boolean ispandi(){int ch[] = Arrays.copyOf(array, 10);Arrays.sort(ch);for (int i = 0; i < ch.length; i++){if (ch[i] != i){return false;}}return true;}}


answer:  16695334890
time:  143


0 0
原创粉丝点击