由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。
比如:
6 x 210 = 1260
8 x 473 = 3784
27 x 81 = 2187
都符合要求。
如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,请输出剩余的满足要求的算式且按照第一个因数从小到大排列,第一个因数相等,按照第二个因数从小到大排列
标签:
由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。
比如:
6 x 210 = 1260
8 x 473 = 3784
27 x 81 = 2187
都符合要求。
如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,请输出剩余的满足要求的算式且按照第一个因数从小到大排列,第一个因数相等,按照第二个因数从小到大排列
6 x 210 = 1260
8 x 473 = 3784
27 x 81 = 2187
分析:注意看清楚题,题上的一些数式不输出,我已经wrong了很多次,才过去,注意输出的乘号和空格的问题,乘号是小写的x而不是*,这次得长记性啦
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<string> using namespace std; int a[10],b[10]; int count1,count2; bool zhuanhua(int a[],int b[]) { int flag=1; sort(a,a+count1); sort(b,b+count2); for(int i=0;i<count2;i++) { if(a[i]==a[i+1]) { for(int j=i;j<count2;j++) { a[j]=a[j+1]; i--; count2--; } } } if(count1!=count2) return false; for(int i=0;i<count1;i++) { if(a[i]!=b[i]) { flag=0; } } if(flag) { return true; } else return false; } int main() { int sum; for(int i=1;i<10;i++) { for(int j=100;j<=999;j++) { sum=i*j; count1=0; count2=0; if(sum<1000||sum>9999||sum==1260||sum==3784) { continue; } a[count1++]=i%10; a[count1++]=j%10; a[count1++]=j/10%10; a[count1++]=j/100; b[count2++]=sum%10; b[count2++]=sum/10%10; b[count2++]=sum/100%10; b[count2++]=sum/1000; if(count1!=count2&&count2!=4) continue; if(zhuanhua(a,b)) printf("%d x %d = %d\n",i,j,sum); } } for(int i=10;i<100;i++) { for(int j=i;j<=100;j++) { count1=0;count2=0; sum=i*j; if(sum<1000|sum>9999||sum==2187) continue; a[count1++]=i%10; a[count1++]=i/10; a[count1++]=j%10; a[count1++]=j/10; b[count2++]=sum%10; b[count2++]=sum/10%10; b[count2++]=sum/100%10; b[count2++]=sum/1000; if(count1!=count2&&count2!=4) continue; if(zhuanhua(a,b)) printf("%d x %d = %d\n",i,j,sum); } } return 0; }
标签:
原文地址:http://blog.csdn.net/qq_qingtian/article/details/44459425