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.
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.
61 62 0
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("\n"); 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\n",i*n,i,n);
40 flag=0;
41 }
42 }
43 if(flag)
44 printf("There are no solutions for %d.\n",n);
45
46 }
47 return 0;
48 }