|
发表于 2018-7-29 16:28:34
|
显示全部楼层
- import java.util.Arrays;
- public class PandigitalProducts{
- public static void main(String[] args){
- int[] m = new int[3];
- int[] n = new int[4];
- int[] result = new int[10];
- int[] consequence = new int[10];
- int r = 0,sum = 0;
- int count_1 = 0,count_2 = 0,count_3 = 0;
- int i = 0;
- for(int a = 1;a < 100;a ++){
- count_1 = Record(m,a);
- if(Repeat(m,count_1)){
- continue;
- }
- for(int b = 101;b < 10000;b ++){
- count_2 = Record(n,b);
- if( Repeat(n,count_2) ){
- continue;
- }
- if(Pandigital(m,n,count_2) ){
- r = a * b;
- count_3 = Record(result,r);
- if(Repeat(result,count_3) || count_1+count_2+count_3 != 9){
- continue;
- }
- if(Pandigital(result,m,count_1) && Pandigital(result,n,count_2)){
- Arrays.sort(consequence);
- if(Arrays.binarySearch(consequence,r) < 0){
- sum += r;
- }
- System.out.println(a+"*"+b+"="+r);
- consequence[i++] = r;
- }
- }
- else{
- continue;
- }
- }
-
- }
- System.out.println("去掉重复数字的和为"+sum);
- }
- public static int Record(int[] record,int x){
- int count = 0;
- for(int i = 0;i < record.length;i ++){
- record[i] = 0;
- }
- int temp = x;
- for(int i = 0;temp != 0;i ++){
- record[i] = temp % 10;
- count++;
- temp /= 10;
- }
- return count;
- }
- public static boolean Pandigital(int[] record,int[] judge,int count){
- boolean flag = true;
- top:for(int i = 0;i < count;i ++){
- for(int j = 0;j < record.length;j ++){
- if(judge[i] == record[j]){
- flag = false;
- break top;
- }
- }
- }
- return flag;
- }
- public static boolean Repeat(int[] a,int count){
- boolean flag = false;
- for(int i = 0;i < count-1;i ++){
- for(int j = i+1;j < count;j ++){
- if(a[i] == a [j] && !flag){
- flag = true;
- break;
- }
- }
- }
- for(int i = 0;i < count;i ++){
- if(a[i] == 0){
- flag = true;
- }
- }
- return flag;
- }
- }
复制代码
4*1738=6952
4*1963=7852
12*483=5796
18*297=5346
27*198=5346
28*157=4396
39*186=7254
42*138=5796
48*159=7632
去掉重复数字的和为45228 |
|