暴力枚举:
i从10000开始找(因为N最小为2,所以i一定大于i/N,一定是五位数),到98765,如果是N的倍数就开始判断i和i/N是否为不重复数,是->输出,不是->继续找;
注意:
① 0的位置及个数,如果i/N是四位数,那么0要添加在分母前面;
②输出格式格式格式,我就卡这了,输入0之后不输出空行;
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
//int a[10]={0,1,2,3,4,5,6,7,8,9};
int main()
{
int N,frist=0;
while(~scanf("%d",&N)&&N){
if(frist++) cout<<endl;
int flag=0;
for(int i=10000;i<=98765;i++){
int b[10]={0}; //标记是否用过,每一次都要重新赋0
if(i%N==0&&i/N>=1234){
int n=i; //i值不能变,所以用n替代
while((n/10)&&!b[n%10]){
b[n%10]=1;n=n/10;
}
if(!b[n%10]){
b[n%10]=1;n/=10;
}
if(n||(b[0]&&i/N<10000))//0用过但分母是四位数
continue;
else{
if(i/N<10000) b[0]=1;
n=i/N;int j=1;
while((n/10)&&!b[n%10]) {
b[n%10]=1;n=n/10;j++;
}
if(!b[n%10]){
b[n%10]=1;n/=10;
}
if(!n&&j==4){
cout<<i<<" / 0"<<i/N<<" = "<<N<<"\n";
flag=1;
}
else if(!n) {
cout<<i<<" / "<<i/N<<" = "<<N<<"\n";
flag=1;
}
}//else
}//if
}//for
if(!flag) cout<<"There are no solutions for "<<N<<".\n";
}//while
return 0;
}