24h購物| | PChome| 登入
2012-05-15 22:17:18| 人氣2,777| 回應1 | 上一篇 | 下一篇

[UVA] 725 - Division

推薦 0 收藏 0 轉貼0 訂閱站台

  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
le N le 79$. 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


懶得修改加速了, 因此有很糟的速度

#include <stdio.h>

int main() {
    int n, i, j, first = 1;
    while(scanf("%d", &n) == 1 && n) {
        if(!first)
            puts("");
        first = 0;
        int flag = 0;
        for(i = 1234; i <= 99999; i++) {
            if(i*n > 99999)
                break;
            char used[10] = {}, str[10], flag1 = 0;
            sprintf(str, "%05d", i);
            for(j = 0; str[j]; j++)
                used[str[j]-'0']++;
            sprintf(str, "%05d", i*n);
            for(j = 0; str[j]; j++)
                used[str[j]-'0']++;
            for(j = 0; j < 10; j++)
                if(used[j] != 1)
                    flag1 = 1;
            if(!flag1) {
                flag = 1;
                printf("%05d / %05d = %d\n", i*n, i, n);
            }
        }
        if(!flag)
            printf("There are no solutions for %d.\n", n);
    }
    return 0;
}

台長: Morris
人氣(2,777) | 回應(1)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 496 - Simply Subsets
此分類上一篇:[UVA] 10070 - Leap Year or Not Leap Year and ...

春藥
很讚的分享~~
2020-02-20 14:02:08
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文