Description
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 . 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
解题思路:
题目的大意是给定一个数让我们给出两个数相除可以得到这个数,对于这两个数我们也有一定的要求,其中的被除数要求是一位五位数,除数可以是四位数(这是它的第一位数是0)也可以是五位数。然后这两个数的组成要求用到0-9的所有数。首先我们可以确定我们查找的范围(1234-98765),这时我们查找的是除数,然后对于属于这个范围内的每一个数都求出它的个十百千万位对于每次出现的数字都把它用一个数组标记一下。然后我们将这个数与我们从键盘上输入的数进行相乘一次,得到另一个数,对于这个数我们也要求它不要 大于98765,如果大于就break掉,再求出这个数的个十百千万位,也做一下标记。最后我们判断是否从0-9的每一个数都出现了一次,只有当他们都出现了一次的时候我们找到的这两个数才是符合条件的。
程序代码:
#include <iostream> #include <cstring> using namespace std; int book[10];//标记数组 int main() { int a,b,c,d,e,f,g,h,i,j,w,x,y,s,k=0; while(cin>>x&&x) { if(k==0) k=1; else cout<<endl; s=0; for(int v=1234;v<=98765;v++) { memset(book,0,sizeof(book)); a=v%10;b=v/10%10;c=v/100%10;d=v/1000%10;e=v/10000%10; w=x*v; if(w>98765) break; f=w%10;g=w/10%10;h=w/100%10;i=w/1000%10;j=w/10000%10; book[a]++;book[b]++;book[c]++;book[d]++;book[e]++;//标记 book[f]++;book[g]++;book[h]++;book[i]++;book[j]++;//标记 for(y=0;y<10;y++) { if(book[y]>1) break; } if(y==10)//此时的0-9的每个数都出现了一次 { s++; cout<<j<<i<<h<<g<<f<<" / "<<e<<d<<c<<b<<a<<" = "<<x<<endl; } else continue; } if(s==0) cout<<"There are no solutions for "<<x<<"."<<endl; } return 0; }