[UVA] 725 - Division
Division
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 . 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;
}
很讚的分享~~