uva725
我到洛谷上看了一篇题解,照着打了一遍,结果tle了,不知道是哪里出了问题。
我抄的
#include <bits/stdc++.h>
using namespace std;
int n, f[10];
int judge(int x, int y)
{
memset(f, 0, sizeof(f));
int cnt = 0;
while (x != 0)
{
f[x % 10]++;
x /= 10;
cnt++;
}
if (cnt == 4)
{
f[0]++;
}
while (y != 0)
{
f[y % 10]++;
y /= 10;
}
for (int i = 0; i < 10; i++)
{
if (f[i] != 1)
return 0;
else
return 1;
}
}
int main()
{
int num = 0;
while (cin >> n && n)
{
if (num++)
cout << "\n";
int flag = 0;
for (int f = 0; f <= 9; f++)
for (int g = 0; g <= 9; g++)
for (int h = 0; h <= 9; h++)
for (int i = 0; i <= 9; i++)
for (int j = 0; j <= 9; j++)
if (judge(f * 10000 + g * 1000 + h * 100 + i * 10 + j, n * (f * 10000 + g * 1000 + h * 100 + i * 10 + j)))
cout << n * (f * 10000 + g * 1000 + h * 100 + i * 10 + j) << " / " << f << g << h << i << j << " = " << n << '\n',
flag = 1;
if (flag == 0)
{
cout << "There are no solutions for " << n << ".\n";
}
}
return 0;
}
源代码
#include<iostream>
#include<cstring>
using namespace std;
int n,f[10];
int judge(int x,int y)
{
memset(f,0,sizeof(f));//清0
int cnt=0;
while(x!=0)
{
f[x%10]++;
x/=10;
cnt++;
}
if(cnt==4)//注意0!
f[0]++;
while(y!=0)
{
f[y%10]++;
y/=10;
}
for(int i=0;i<10;i++)//满足0~9都各有1个
if(f[i]!=1)
return 0;
return 1;
}
int main()
{
int num=0;
while(cin>>n&&n)
{
if(num++)
cout<<"\n";
int flag=0;
//枚举abcde  ̄△ ̄||
for(int i=0;i<=9;i++)
for(int j=0;j<=9;j++)
for(int k=0;k<=9;k++)
for(int l=0;l<=9;l++)
for(int m=0;m<=9;m++)
if(judge(m+l*10+k*100+j*1000+i*10000,n*(m+l*10+k*100+j*1000+i*10000)))
cout<<n*(m+l*10+k*100+j*1000+i*10000)<<" / "<<i<<j<<k<<l<<m<<" = "<<n<<'\n',flag=1;//输出+标记
if(flag==0)//没找到
cout<<"There are no solutions for "<<n<<".\n";
}
return 0;
}