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
1 #include <stdio.h> 2 #include <cstring> 3 int m[10]; 4 int panduan(int a,int b) 5 { 6 if(a>100000) 7 return 0; 8 memset(m,0,sizeof(m)); 9 //for(int i=0;i<10;i++) 10 // m[i]=0; 11 if(b<10000) 12 m[0]; //容易忘记13 while(a) 14 { 15 m[a%10]=1; //每一位都标记 16 a=a/10; //标记了就除掉一位 17 } 18 while(b) 19 { 20 m[b%10]=1; 21 b=b/10; 22 } 23 int sum=0; 24 for(int j=0; j<10; j++) 25 sum+=m[j]; 26 return sum==10; //当sum等于10才返回 27 } 28 int main() 29 { 30 int n,k=0; 31 while(scanf("%d",&n)==1&&n) 32 { 33 if(k>0) printf(" "); k++; //输出格要求 34 int flag=1; 35 for(int i=1234; i<1000000; i++) 36 { 37 if(panduan(n*i,i)) 38 { 39 printf("%d / %05d = %d ",i*n,i,n); 40 flag=0; 41 } 42 } 43 if(flag) 44 printf("There are no solutions for %d. ",n); 45 46 } 47 return 0; 48 }