简单枚举 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,去判断是否存在重复数字即可。
代码
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; }
相关文章推荐
- 简单枚举UVa725-division (abcde / fghij = n)
- UVA 725 Division(暴力枚举 简单)
- uva 725 简单枚举
- UVa 725 Division --- 简单枚举
- UVa 725 简单枚举+整数转换为字符串
- UVa 725 简单枚举+整数转换为字符串
- UVA 725 简单枚举
- uva 725 Division 简单枚举
- UVA725 Division【暴力枚举】
- UVa 725----Division(枚举)
- uva 725 Division(暴力枚举)
- uva 725 Division(暴力枚举) 解题心得
- UVa 725 Division(枚举)
- uva 725 DIVISION (暴力枚举)
- UVA 11227 The silver bullet.(简单题:枚举经过最多的点的直线)
- 暴力枚举 UVA 725 Division
- UVA 725 Division(hash+枚举)
- uva 10167 - Birthday Cake(最简单枚举)
- Birthday Cake (uva10167简单暴力枚举)
- UVa 216 - Getting in Line 回溯,简单递归枚举