传送门:点击打开题目链接
题意:输入一个数字n,按从小到大输出所有形如abcde / fghij = n的表达式,其中a - j 恰好为数字0-9的一个排列(可以有前导零)。2<= n <= 79
思路:一般都会想到暴力枚举,但是会时间复杂度太高。不如只枚举abcde,然后根据n算出fghij,过程比较麻烦,但是知道思路后属于一道比较简单的题
直接上代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
int n;
int sum = 0;int ok = 0;
while(scanf("%d",&n) && n){
if(ok == 0)
ok = 1;
else
printf("\n");
int a,b;
int Flag = 0;
for(int i = 1000; i < 100000 ;i ++){
int ansa[10];
int ansb[10];
int vis[10];
memset(ansa,0,sizeof(ansa));
memset(ansb,0,sizeof(ansb));
memset(vis,0,sizeof(vis)) ;
a = i;
if(a % n == 0){
sum = 0;
b = a / n;
int tempa = a;
int tempb = b;
int ii = 0 ,jj = 0;
while(tempa){
int ans = tempa % 10;
ansa[ii++] = ans;
tempa /= 10;
vis[ans] ++;
}
while(tempb){
int ans = tempb % 10;
ansb[jj++] = ans;
tempb /= 10;
vis[ans] ++;
}
if(ii+jj==9) vis[0]++;
int flag = 0;
for(int z = 0 ;z < 10 ; z++){
if(vis[z] != 1){
flag = 1;
break;
}
}
if(!flag){
Flag = 1;
for(int iii = 4 ; iii >= 0 ; iii --){
sum+=ansa[iii];
printf("%d",ansa[iii]);
}
printf(" / ");
for(int jjj = 4 ; jjj >= 0 ; jjj --){
sum+=ansb[jjj];
printf("%d",ansb[jjj]);
}
printf(" = %d",n);
printf("\n");
}
}
}
if(Flag == 0){
printf("There are no solutions for %d.\n",n);
}
}
return 0;
}