Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
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
Miguel Revilla
2000-08-31
这一题用暴力绝对超时!但是你可以分析一半,让他乘以商再
判断他的积是否超过一百万!还要判断他们是否相等,这时候用C++的stl会轻松很多,很好的一个题目!
#include<cstdio>
#include<iostream>
#include<set>
#include<cstring>
using namespace std;
int check(int n,int m)
{
int i;
char s1[10];
char s2[10];
set<int>st;
sprintf(s1,"%05d",n);
sprintf(s2,"%05d",m);
// cout<<s1<<" "<<s2<<endl;
for(i=0;i<strlen(s1);i++)
{
st.insert(s1[i]-'0');
}
for(i=0;i<strlen(s2);i++)
{
st.insert(s2[i]-'0');
}
if(st.size()==10)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int p,i,f=0,success=0;
while(cin>>p,p)
{
success=0;
if(f!=0)
{
cout<<endl;
}
f++;
for(i=1234;i<=98765;i++)
{
if(i*p>=100000)
{
break;
}
if(check(i,i*p))
{
success=1;
printf("%05d / %05d = %d\n",i*p,i,p);
}
}
if(!success)
{
cout<<"There are no solutions for "<<p<<"."<<endl;
}
}
}