Division
Division |
Write a program that finds and displays all pairs of 5-digit numbers thatbetween them use the digits0 through 9 once each, such that thefirst number divided by the second is equal to an integerN, where .That is,
abcde / fghij = N
where each letter represents a different digit. The first digit of one ofthe 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 terminatethe program.
Output
Your program have to display ALL qualifying pairs of numerals, sorted byincreasing 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 mustwrite ``There are no solutions forN.". Separate the output for twodifferent 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,判定是否存在a / b = n;其中a和b均为5位数,这十位数是0-9的一个排列
题解:
1.直接 暴力枚举即可,枚举b: 1234~98765, 那么a = b * n;
2. 此时判断组成a和b的十位数是否为0~9的一个排列即可(每个数字只出现一次,且不能重复)
代码如下:
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int used[10];
bool Judge(int a, int b) // a / b = N
{
if(a > 98765) return false;
for(int i = 0; i < 10; i++) used[i] = 0;
if(b < 10000) used[0] = 1;
while(b){
used[b%10]++;
b /= 10;
}
while(a){
used[a%10]++;
a /= 10;
}
for(int i = 0; i < 10 ; i++)
if(used[i] != 1) return false;
return true;
}
int main()
{
int N, icase = 0;
int a, b;
while(cin>>N && N)
{
if(icase++) cout<<endl;
int a, b;
bool exist = false;
for(b = 1234; b <= 98765; b++){
a = b * N;
if(Judge(a, b)) { exist = true; printf("%05d / %05d = %d\n", a, b, N); }
}
if(!exist) printf("There are no solutions for %d.\n", N);
}
return 0;
}