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
解题思路:首先根据题意表明除号两边分别都有五个数,并且每一个数字都不能相同,所以可以使用一个for循环从1234开始到98765结束。在循环里面要用一个数组作为标记,判断数字是否重复出现了,这里还要注意的是输出的格式这里很容易出错。
程序代码:
#include<stdio.h>
#include<string.h>
//using namespace std;
int main()
{
int s=0;
int m,n,t,k,book[10];
while(scanf("%d",&n)==1&&n)
{
if(s++>0) printf("
");
int floag=0;
for(t=1234;t<=98765;t++)
{
memset(book,0,sizeof(book));
m=n*t;
if(m>98765)
break;
int a=m/10000;
int b=m/1000%10;
int c=m/100%10;
int d=m/10%10;
int e=m%10;
int f=t/10000;
int g=t/1000%10;
int h=t/100%10;
int i=t/10%10;
int j=t%10;
book[a]++;
book[b]++;
book[c]++;
book[d]++;
book[e]++;
book[f]++;
book[g]++;
book[h]++;
book[i]++;
book[j]++;
for(k=0;k<10;k++)
{
if(book[k]>1)
break;
}
if(k==10) {printf("%d%d%d%d%d / %d%d%d%d%d = %d
",a,b,c,d,e,f,g,h,i,j,n);floag=1;}
//else continue;
}
if(floag==0) printf("There are no solutions for %d.
",n);
}
return 0;
}
很容易出错