| Division |
Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0through 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.
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.
61 62 0
There are no solutions for 61. 79546 / 01283 = 62 94736 / 01528 = 62
暴力求解,枚举第二个数
#include<iostream>
#include <stdio.h>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
int a[11],n;
int is(int i)
{
if(a[i]==1)return 1;
return 0;
}
int judge(int i,int j,int k,int o,int p)
{
int x,s,s1,s2;
memset(a,0,sizeof(a));
a[i]++;a[j]++;a[k]++;a[o]++;a[p]++;
s1=10000*i+1000*j+100*k+10*o+p;
s=s1*n;
s2=s;
while(s){
a[s%10]++;
s/=10;
}
if(is(1)&&is(2)&&is(3)&&is(4)&&is(5)&&is(6)&&is(7)&&is(8)&&is(9)&&is(0))
{printf("%d / %05d = %d\n",s2,s1,n);return 1;}
else return 0;
}
int main()
{
int i,j,k,o,p,t=0;
while(scanf("%d",&n),n){
t++;
if(t!=1)puts("");
int e=0;
for(i=0; i<=9; i++)
for(j=0; j<=9; j++)
for(k=0; k<=9; k++)
for(o=0; o<=9; o++)
for(p=0; p<=9; p++)
if(judge(i,j,k,o,p))
e=1;
if(!e)printf("There are no solutions for %d.\n",n);
}
return 0;
}原文:http://blog.csdn.net/u014705854/article/details/43057249