链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666
725 Division
Write a program that finds and displaysall pairs of 5-digit numbers that between them use the digits 0 through 9 onceeach, such thatthe first number dividedby the secondis equal to an integer N , where 2 ≤ N ≤ 79. That is,
abcde
= N
fghij
where each letter representsa different digit. The first digit of one of the numerals is allowedto be zero.
Input
Each line of the input file consists of a valid integer N . An inputof zero is to terminate the program.
Output
Your program have to display ALL qualifying pairs of numerals, sortedby increasing numerator(and, of course,denominator).
Your output should be in the following generalform:
xxxxx / xxxxx = N xxxxx / xxxxx = N
.
.
In case there are no pairs of numerals satisfyingthe 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
题意:
键入 N ,求由 0~9 组成的 abcde / fghij = N 满足的项
思路:
直接暴力走过,可知 abcde 组成的数字是 12345 - 98765 ,拟写一个判断 0 - 9 的数字是否都出现的函数即可。
但是卡的事 是格式!
AC CODE:
#include<stdio.h>
#include<cstring>
#include<algorithm>
#define HardBoy main()
#define ForMyLove return 0;
using namespace std;
const int MYDD = 1103;
int GetBit[10];/*存储数位的数字*/
bool Judge(int x, int y) {
memset(GetBit, 0, sizeof(GetBit));
while(x) {/*Bug 2016年12月5日19:07:30 -> 写成 if 判断*/
GetBit[x%10]++;
x /= 10;
}
while(y) {
GetBit[y%10]++;
y /= 10;
}
for(int i = 1; i <= 9; i++) {/*不必判断 0 */
if(GetBit[i] != 1) {
return false;
}
}
return true;
}
int HardBoy {
int n;
while(scanf("%d", &n) && n) {
int flag = 0;
for(int j = 12345; j <= 98765; j++) {
if(j%n == 0) {
int zi = j, mu = j/n;
if(Judge(zi, mu)) {
printf("%d / %05d = %d\n", j, j/n, n);
flag = 1;
}
}
}
if(!flag) printf("There are no solutions for %d.\n", n);
printf("\n");
}
ForMyLove
}