32 Pandigital products - Project Euler -

原创 2015年11月17日 18:01:22
package xxx.xxx.xxx;


import java.util.ArrayList;
import java.util.HashSet;


/*
 * We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.


 The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.


 Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.


 HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
 */
public class PandigitalProducts {


private static char[] is = new char[] { '1', '2', '3', '4', '5', '6', '7',
'8', '9' };
private static int total;
private static int m = 9;
private HashSet<Integer> products = new HashSet<Integer>();




private void plzh(String s, ArrayList<Integer> iL, int m) {
if (m == 0) {
// System.out.println(s);
this.isPandigitalEquation(s);
total++;
return;
}
ArrayList<Integer> iL2;
for (int i = 0; i < is.length; i++) {
iL2 = new ArrayList<Integer>();
iL2.addAll(iL);
if (!iL.contains(i)) {
String str = s + is[i];
iL2.add(i);
plzh(str, iL2, m - 1);
}
}
}

/*
* 1位数*4位数的结果:最短4位数,最长5位数
* 2位数*2位数的结果:最短3位数,最长4位数
* 2位数*3位数的结果:最短4位数,最长5位数
* 2位数*4位数的结果:最短5位数,最长6位数
*/
private void isPandigitalEquation(String str){
int a = Integer.valueOf(str.substring(0, 1));
int b = Integer.valueOf(str.substring(1, 5));
int c = Integer.valueOf(str.substring(5));
if(a*b==c){
products.add(c);
}else{
int a1 = Integer.valueOf(str.substring(0, 2));
int b1 = Integer.valueOf(str.substring(2, 5));
int c1 = Integer.valueOf(str.substring(5));

if(a1*b1==c1){
products.add(c1);
}
}
}




private int isPandigitalEquation3(String str){

int a = Integer.valueOf(str.substring(0, 1));
int b = Integer.valueOf(str.substring(1, 4));
int c = Integer.valueOf(str.substring(4));


if(a*b==c){
System.out.println(str);
System.out.println(c);
return c;
}else{
int a1 = Integer.valueOf(str.substring(0, 2));
int b1 = Integer.valueOf(str.substring(2, 5));
c = Integer.valueOf(str.substring(5));

if(a1*b1==c){
System.out.println(str);
System.out.println(c);
return c;
}
}

return 0;
}

public static void main(String[] args) {
/*
long startTime = System.currentTimeMillis();
ArrayList<Integer> iL = new ArrayList<Integer>();
new PandigitalProducts().plzh("", iL, m);
System.out.println("total : " + total);
long endTime = System.currentTimeMillis();
System.out.println("execution time " + (endTime - startTime + "ms"));
*/



long startTime = System.currentTimeMillis();
ArrayList<Integer> iL = new ArrayList<Integer>();
PandigitalProducts p =new PandigitalProducts();
p.plzh("", iL, m);

HashSet<Integer> temp = p.products;
System.out.println(temp);
int sum = 0;
for(int t:temp){
sum+=t;
}
System.out.println(sum);
long endTime = System.currentTimeMillis();
System.out.println("execution time " + (endTime - startTime + "ms"));
}


}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Project Euler Problem 32 Pandigital products

Pandigital products Problem 32 We shall say that an n-digit number is pandigital if it makes use ...

[Euler]Problem 32 - Pandigital products

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o...

Project Euler Problem 104 Pandigital Fibonacci ends

Pandigital Fibonacci ends Problem 104 The Fibonacci sequence is defined by the recurrence relatio...

<MEMORY>Project Euler NO32

如果一个n位数使用了1到n中每个数字且只使用了一次,我们称其为pandigital。例如,15234这个五位数,是1到5pandigital的。 7254是一个不寻常的数,因为:39 × 186...

project euler problem 5

  • 2010-12-18 10:58
  • 18KB
  • 下载

Project Euler:Problem 49 Prime permutations

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...

Project Euler:Problem 22 Names scores

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-t...

Project Euler 013 Large sum

题意:求1010个5050位数的和的前1010位。 分析:裸的高精度,或者double啊啥的应该也是能过的吧。#include #define ll long long#define pii std...

<MEMORY>Project Euler NO57

2的平方根可以被表示为无限延伸的分数:  2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213... 将其前四次迭代展开,我们得到: 1 +...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)