UVA - 725(水题)

来源:互联网 发布:linux系统模拟下载 编辑:程序博客网 时间:2024/04/30 17:13

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,
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 <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <queue>#include <cmath>using namespace std;int main(){    int n, a[10], first = 1;    while (scanf ("%d", &n) != EOF) {        if (n == 0) break;        if (first) {            first = 0;        } else {            printf ("\n");        }        int num1 = 1, num2 = 1;        int side = 98765 / n;        int flag1 = 0;        for (num1 = 1234; num1 <= side; num1++) {            int flag = 1;            num2 = num1 * n;            a[0] = num2 / 10000;            a[1] = num2 / 1000 % 10;            a[2] = num2 / 100 % 10;            a[3] = num2 / 10 % 10;            a[4] = num2 % 10;            a[5] = num1 / 10000;            a[6] = num1 / 1000 % 10;            a[7] = num1 / 100 % 10;            a[8] = num1 /10 % 10;            a[9] = num1 % 10;            for (int i = 0; i < 10; i++) {                for (int j = 0; j < 10; j++) {                    if (j != i && a[i] == a[j]) {                        flag = 0;                    }                }            }            if (flag) {                printf ("%d%d%d%d%d / %d%d%d%d%d = %d\n", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], n);                flag1 = 1;            }        }        if (flag1 == 0) {            printf ("There are no solutions for %d.\n", n);        }    }    return 0;}
0 0
原创粉丝点击