UVa725

/*
本题是使用暴力搜索的方法解决的,关键是分析问题的特点
针对除数b进行暴力搜索,其搜索范围是(1000,100000)
被除数a=b*n,然后判断a和b是不是把0-9的数字全使用了,而且只使用一次
这中间有一个问题,就是0可以没用过,然后在b的前面补0即可。
*/
#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
 
 
bool ju(int a,int b){
    int num[10],tmp;
    memset(num,0,sizeof(num));
    tmp=a;while(tmp){num[tmp%10]+=1;tmp/=10;}
    tmp=b;while(tmp){num[tmp%10]+=1;tmp/=10;}
    for(int i=1;i<10;++i)if(num[i]!=1)return false;
    return (num[0]==0&&a>10000&&b<10000)||(num[0]==1&&b>10000);
}
int main()
{
    int n,kase=0;
    while(scanf("%d",&n)&&n){
        bool flag=false;
        if(kase++)printf("\n");
        for(int i=1000;i<100000;++i){
            if(ju(i*n,i)){
                flag=true;
                printf("%d / %05d = %d\n",i*n,i,n);
            }
        }
        if(!flag)printf("There are no solutions for %d.\n",n);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shihongliang1993/article/details/74315811
今日推荐