思路:
本题可以通过简单枚举来实现,不过需要注意的是只需要枚举分子或分母即可,无需全部枚举。
具体代码:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int N;
vector<int> digits;
bool first=true;
for(cin>>N;N!=0;cin>>N)
{
if(first)
first=false;
else
cout<<endl;
bool haveAns=false;
for(int i=0;i<=9;++i)
for(int j=0;j<=9;++j)
{
if(i==j) continue;
for(int k=0;k<=9;++k)
{
if(k==i||k==j) continue;
for(int l=0;l<=9;++l)
{
if(l==i||l==j||l==k) continue;
for(int m=0;m<=9;++m)
{
if(m==i||m==j||m==k||m==l) continue;
else
{
int t=N*(i*10000+j*1000+k*100+l*10+m);
digits.clear();
while(t>0)
{
digits.push_back(t%10);
t/=10;
}
if(digits.size()>5)
continue;
else if(digits.size()<5)
digits.push_back(0);
digits.push_back(i);
digits.push_back(j);
digits.push_back(k);
digits.push_back(l);
digits.push_back(m);
int occur[10];
fill(occur,occur+10,0);
bool ok=true;
for(int i=0;i<digits.size();++i)
{
++occur[digits[i]];
if(occur[digits[i]]>1)
{
ok=false;
break;
}
}
if(ok)
{
haveAns=true;
for(int i=4;i>=0;--i)
{
cout<<digits[i];
}
cout<<" / ";
cout<<i<<j<<k<<l<<m<<" = "<<N<<endl;
}
}
}
}
}
}
if(!haveAns)
{
cout<<"There are no solutions for "<<N<<"."<<endl;
}
}
return 0;
}