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
暴力解决,不过要注意输出时最后一组数据不能多出空行
#include"iostream" #include"cstring" using namespace std; int n; int book[100]= {0}; bool judgeint c,int cc,int ccc) { memsetbook,0,sizeofbook)); ifccc==0) book[0]++; int t,f; t=c; f=0; whilet/10>0) { book[t%10]++; ifbook[t%10]>1) { f=1; } t/=10; } book[t]++; ifbook[t]>1) { f=1; } t=cc; whilet/10>0) { book[t%10]++; ifbook[t%10]>1) { f=1; } t/=10; } book[t]++; ifbook[t]>1) { f=1; } iff) return false; return true; } void go) { int i,flag; flag=0; fori=1111; i<=99999; i++) { ifi*n>98765) break; ifjudgei,i*n,1)) { ifi*n)/10000==0) continue; ifi/10000==0&&judgei,i*n,0)) { cout<<i*n<<" / 0"<<i<<" = "<<n<<endl; flag=1; } ifjudgei,i*n,1)&&i/10000!=0) { cout<<i*n<<" / "<<i<<" = "<<n<<endl; flag=1; } } } ifflag==0) cout<<"There are no solutions for "<<n<<'.'<<endl; } int main) { int x=1; whilecin>>n&&n) { ifx>1) cout<<endl; x++; go); } return 0; }