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
题意:输入一个数n,从小到大输出它的abcde / fghij = n的类型的式子(注意空格,可以有前导0)a-j是一个0到9的排列。如果没有则输出There are no solutions for 61. 格式要求:每测试一组案例,换一空行。(这输出格式也是RLGL)
解题思路:枚举,但是是从被除数枚举,通过相乘算出除数。然后就只需要判断除数是否大于100000,a-j是否相等。
代码如下:(本来想自己写的,就是自己不会用标记,最后还是没有坚持下来,还是看了别人的博客,然后仿写了。)
想问个问题,用 memse(m,0,sizeof(m)) 清零数组m,为什么一定是sizeof(m), 不可以用m的长度10,因为当我用10的时候就连案例都通不过了....
求告知,没错我就是这么菜.....(⊙﹏⊙)b
#include <stdio.h>
#include <cstring>
int m[];
int panduan(int a,int b)
{
if(a>)
return ;
memset(m,,sizeof(m));
//for(int i=0;i<10;i++)
// m[i]=0;
if(b<)
m[]; //容易忘记 while(a)
{
m[a%]=; //每一位都标记
a=a/; //标记了就除掉一位
}
while(b)
{
m[b%]=;
b=b/;
}
int sum=;
for(int j=; j<; j++)
sum+=m[j];
return sum==; //当sum等于10才返回
}
int main()
{
int n,k=;
while(scanf("%d",&n)==&&n)
{
if(k>) printf("\n"); k++; //输出格要求
int flag=;
for(int i=; i<; i++)
{
if(panduan(n*i,i))
{
printf("%d / %05d = %d\n",i*n,i,n);
flag=;
}
}
if(flag)
printf("There are no solutions for %d.\n",n); }
return ;
}