由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成.
比如:210*6=1260,8*473=3784,27*81=2187都符合要求
如果满足乘法交换律的算式算作同一种情况,那么,一共有多少种满足?
#include <stdio.h> int issame(int a,int b,int c,int d) { if(a == b || a == c || a == d ) return 1; if(b == c || b == d || c == d) return 1; return 0; } int isincluded(int t,int a,int b,int c,int d) { int a1,b1,c1,d1; int mark = 0; //取每位数 a1 = t%10; t/=10; b1 = t%10; t/=10; c1 = t%10; d1 = t/10; if(!issame(a1,b1,c1,d1)) { if(a1 == a || a1 == b || a1 == c || a1 == d) mark ++; if(b1 == a || b1 == b || b1 == c || b1 == d) mark ++; if(c1 == a || c1 == b || c1 == c || c1 == d) mark ++; if(d1 == a || d1 == b || d1 == c || d1 == d) mark ++; if(mark == 4) return 1; } return 0; } int main() { int a,b,c,d,t; int count = 0; //a*bcd for(a=2;a<=9;a++) for(b=1;b<=9;b++) for(c=0;c<=9;c++) for(d=0;d<=9;d++) { if(!issame(a,b,c,d)) { t = a*(100*b+10*c+d); if(t/1000 > 0 && isincluded(t,a,b,c,d))//4位数,且由a,b,c,d组成 { printf("%d*%d=%d ",a,t/a,t); count ++; } } } //ab*cd(ab<cd) for(a=1;a<=9;a++) for(b=0;b<=9;b++) for(c=2;c<=9;c++)//判断语句不可写为:c>a && c<=9,否则a=2时就会跳过搜索了,导致搜索到的最大的数 为1999 for(d=0;d<=9;d++) { if(c > a && !issame(a,b,c,d))//c要大于a { t = (10*a+b)*(10*c+d); if(t/1000 >0 && isincluded(t,a,b,c,d)) { printf("%d*%d=%d ",10*a+b,10*c+d,t); count ++; } } } printf("共计:%d ",count); return 0; }