32 Pandigital products - Project Euler -
来源:互联网 发布:java 读文件为字符串 编辑:程序博客网 时间:2024/05/10 12:39
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"));
}
}
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"));
}
}
0 0
- 32 Pandigital products - Project Euler -
- Project Euler:Problem 32 Pandigital products
- [Euler]Problem 32 - Pandigital products
- Project Euler 38 Pandigital mutiples(枚举)
- Project Euler:Problem 38 Pandigital multiples
- Project Euler:Problem 41 Pandigital prime
- projecteuler---->problem=32----Pandigital products
- Problem 32 Pandigital products (暴力+set)
- No_32:Pandigital products
- PE32-Pandigital products
- Project Euler Problem 32
- Project Euler - Problem 32
- Project Euler problem 32
- Project Euler 32
- Python-Project Euler 32
- Project Euler Problem 32
- project euler 32
- Project Euler
- 普元日记--动态添加控件跳转页(shutcut.html)
- CocoaPods安装和使用及问题:Setting up CocoaPods master repo的认识
- hdu 1385 Minimum Transport Cost(最短路+记录路径)
- Linux I2C驱动分析(一)----I2C架构和总线驱动
- IOS UIButton用法详解
- 32 Pandigital products - Project Euler -
- Postgresql jdbc scheme设置
- Android System Property分析(3):property接口实现
- cout<<和print()的区别
- 一分钟
- 中英文混合排序
- 针对LdrLoadDll下条件断点过滤指定名称DLL加载时断点生效
- UVA 465 Overflow(浮点数的范围)
- .properties文件配置简单数据,解决数据存取以及迭代替换