您的位置:首页 > 其它

简单枚举 uva725

2017-09-02 16:16 393 查看
Division

Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where

‘$2. That is,

abcde / fghij = N

where each letter represents a different digit. The first digit of one of the numerals is allowed to be zero.

Input

Each line of the input file consists of a valid integer N. An input of zero is to terminate the program.

Output

Your program have to display ALL qualifying pairs of numerals, sorted by increasing numerator (and, of course, denominator).

Your output should be in the following general form:

xxxxx / xxxxx = N

xxxxx / xxxxx = N

.

.

In case there are no pairs of numerals satisfying the condition, you must write “There are no solutions for N.”. Separate the output for two different values of N by a blank line.

Sample Input

61

62

0

Sample Output

There are no solutions for 61.

79546 / 01283 = 62

94736 / 01528 = 62

题目大意:输入一个正整数N,要求从小到大的输出形如abcde / fghij = n的表达式。要求a~f为数字0~9,且不能重复(前边可有0)。

思路:如果将a b c d e f g h i j全部遍历的话,复杂度是10!,没有必要,直接枚举

f g h i j,然后算出a b c d e,去判断是否存在重复数字即可。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n,ok=0;   //每一个输出后带一个空行,最后一行不带。
int a[10];
int a1[10];
while(scanf("%d",&n)!=EOF&&n)
{
if(ok==0)
ok=1;
else
cout<<endl;
int flag=0,ans=0;
for(int i=1234;i<=50000;i++)
{
flag=0;
for(int j=0;j<10;j++)
a[j]=0;
int x=i*n,y=i;
if(x>=100000)
continue;
for(int j=0;j<5;j++)
{
a[y%10]++;
y/=10;
}
for(int j=0;j<5;j++)
{
a[x%10]++;
x/=10;
}
for(int j=0;j<10;j++)
{
if(a[j]!=1)
{
flag=1;
break;
}
}
if(flag)
continue;
if(i<10000)
{
cout<<i*n<<" / 0"<<i<<" = "<<n<<endl;
}
else
{
cout<<i*n<<" / "<<i<<" = "<<n<<endl;
}
ans++;
}
if(ans==0)
{
cout<<"There are no solutions for "<<n<<"."<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: