實機體驗,再天天抽M8老王現身說法關鍵字魅力!攝影界美人正妹指定相機款反服貿流血 法官要警交...
2012-05-15 22:17:18 人氣(368) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA] 725 - Division

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;
}

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

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文