24h購物| | PChome| 登入
2012-01-29 14:35:19| 人氣1,544| 回應0 | 上一篇 | 下一篇

[UVA] 471 - Magic Numbers

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

Magic Numbers 

Write a program that finds and displays all pairs of integers tex2html_wrap_inline28 and tex2html_wrap_inline30 such that:

  1. neither tex2html_wrap_inline28 nor tex2html_wrap_inline30 have any digits repeated; and
  2. tex2html_wrap_inline36 , where N is a given integer;

Input and Output

The input file consist a integer at the beginning indicating the number of test case followed by  a blank line. Each test case consists of one line of input containing N. Two input are separated by a blank line.

For each input the output consists of a sequence of zero or more lines each containing tex2html_wrap_inline28 / tex2html_wrap_inline30 = N, where tex2html_wrap_inline48 and N are the integers described above. When there are two or more solutions, sort them by increasing numerator values. Two consecutive output set will separated by a blank line. 

Sample Input

1

1234567890

Sample Output

1234567890 / 1 = 1234567890
2469135780 / 2 = 1234567890
4938271560 / 4 = 1234567890
6172839450 / 5 = 1234567890
8641975230 / 7 = 1234567890
9876543120 / 8 = 1234567890


做法 : 爆搜

#include <stdio.h>
const long long limit = 9876543210LL;
int Check(long long n) {
    char digit[10] = {0}, s[11], i = 0;
    sprintf(s, "%lld", n);
    while(s[i]) {
        digit[s[i]-'0']++;
        if(digit[s[i]-'0'] == 2)
            return 0;
        i++;
    }
    return 1;
}
int main() {
    int T;
    long long n, m, i;
    scanf("%d", &T);
    while(T--) {
        scanf("%lld", &n);
        for(i = 1; n*i <= limit; i++) {
            m = n*i;
            if(Check(i) && Check(m))
                printf("%lld / %lld = %lld\n", m, i, n);
        }
        if(T)    puts("");
    }
    return 0;
}


台長: Morris
人氣(1,544) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10496 - Collecting Beepers [TSP][DP]
此分類上一篇:[UVA] 10706 - Number Sequence

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文