Uva 725 除法

时间:2023-03-10 00:47:31
Uva 725 除法

紫书P182

直接枚举 0~9 的全排列会超时,枚举fghij就可以了,计算出 abcde ,这里有一个新的函数,也可以不用咯,把每一位数据提取出来,while循环可以做到,这里的新的函数是,sprintf(buf,"%5d%5d",abcde,fghij); 格式化提取,把abcde,fghij每一位提取到字符串buf中,不足5位的补0。然后看每一个位是否都有。都有就是一个0~9的全排列。

/*#include <stdio.h>
#include <algorithm> using namespace std; int main()
{
int n;
int a[10] = {0,1,2,3,4,5,6,7,8,9};
while(scanf("%d",&n),n) { bool flag = false;
do { int s = 0;
for(int i=0;i<5;i++) {
s = s*10 + a[i];
}
int t = 0;
for(int i=5;i<10;i++) {
t = t*10 + a[i];
}
if(s%t==0&&s/t==n) {
flag = 1;
printf("%d%d%d%d%d / %d%d%d%d%d = %d\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],n);
} }while(next_permutation(a,a+10)); if(!flag)
printf("There are no solutions for %d.\n",n); } return 0;
}
*/ #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int main() { //freopen("in.txt","r",stdin);
int n;
int kase = ;
char buf[];
while(scanf("%d",&n),n) { if(kase++)
printf("\n");
int cnt = ;
for(int fghij = ;;fghij++) {
int abcde = fghij*n;
sprintf(buf,"%05d%05d",abcde,fghij);
if(strlen(buf)>) break;
sort(buf,buf+);
bool ok = true;
for(int i=;i<;i++) {
if(buf[i]!=''+i) ok = false;
} if(ok) {
cnt++;
printf("%05d / %05d = %d\n",abcde,fghij,n);
} }
if(!cnt)
printf("There are no solutions for %d.\n",n); } return ;
}