UVA OJ-725 Division (暴力求解法)

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 2<=N <=79. That is,git

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 

.ide

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 

Sample Output 
There are no solutions for 61.spa

79546 / 01283 = 62 
94736 / 01528 = 62code

譯文爲:orm

寫一個程序,查找並顯示全部對5位數字,它們之間用數字0 9一次經過,這樣第一個數除以二等於一個整數n,其中2<=n≤79。這是,
ABCDE / fghij = N
每一個字母表明不一樣的數字。一個數字的第一個數字被容許爲零。
輸入
輸入文件的每一行由一個有效的整數N組成,輸入的零是終止程序。
輸出
你的程序要顯示全部符合資格的雙數字,經過增長分子排序(和,固然,分母)。
你的輸出應該是如下的通常形式:
xxxxx / xxxxx = N
xxxxx / xxxxx = N


若是沒有對知足條件的數字,你必須寫「沒有解決方案爲n」。用一條空白線將輸出的2個不一樣的值分別爲2個不一樣的值。
樣本輸入
61
62
0
示例輸出
There are no solutions for 61.blog


79546 / 01283 = 62
94736 / 01528 = 62排序

連接http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35442input

 1 #include<stdio.h>
 2 #include<string.h>
 3 int a[10];//標記數組 
 4 int check(int x, int y) {
 5   if (y > 98765) return 0;//若是x>98765,確定有重複的數字 
 6   memset(a, 0, sizeof(a));//標記數組歸零 
 7   if (x < 10000) a[0] = 1;//四位數是,第一位爲0 
 8   while (x) {//標記x的每位數 
 9     a[x%10] = 1;
10     x /= 10;
11   }
12   while (y) {
13     a[y%10] = 1;
14     y /= 10;
15   }
16   int sum = 0;
17   for (int i = 0; i < 10; i++)
18     if (a[i]) sum++;// 判斷0-9數字是否所有用到 
19   return (sum == 10); 
20 } 
21 int main() {
22   int n, count = 0;
23   while (scanf("%d", &n) && n) {
24     int flag = 1;//
25     if (count++) printf("\n");//輸出格式要求 
26     for (int i = 1234; i < 100000; i++) {//小與1234必有重複數字,暴力枚舉 
27       if (check(i, i*n)) {//
28         printf("%05d / %05d = %d\n", i*n, i, n);//輸出格式 
29         flag = 0;
30       }
31     }
32     if (flag)//
33       printf("There are no solutions for %d.\n", n);//注意這裏有個"." 
34   } 
35 }
相關文章
相關標籤/搜索