UVa 725(简单枚举)

来源:互联网 发布:网络部主要职责 编辑:程序博客网 时间:2024/06/05 16:24

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\le N \le 79$. 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 

61620

Sample Output 

There are no solutions for 61.79546 / 01283 = 6294736 / 01528 = 62


//思路:从b等于1234开始枚举生成a,然后判断a和b是否满足要求(巧用了sprintf先保存到buf,再sort) 还有%05d解决了前导0的问题


AC源码:

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int main(){char buf[15];int kase=0,n;while(cin>>n&&n){int cnt=0;if(kase++)printf("\n");for(int b=1234;;++b){int a=b*n;sprintf(buf,"%05d%05d",a,b);if(strlen(buf)>10)break;sort(buf,buf+10);bool ok=true;for(int i=0;i<10;++i)if(buf[i]!='0'+i)ok=false;if(ok){++cnt;printf("%05d / %05d = %d\n",a,b,n);}}if(!cnt)printf("There are no solutions for %d.\n",n);}return 0;}




原创粉丝点击