如果把数字0到9分配成2个整数(各五位数),现在请你写一支程序找出所有的配对使得第一个数可以整除第二个数,而且商为N(2<=N<=79),也就是:
abcde / fghijk = N
这里每个英文字母代表不同的数字,第一个数字可以为0。
Input
输入包含许多笔待测数据,每列代表一笔待测数据,每笔待测数据包含一个正整数N,N为0时代表输入结束。
Output
对每笔待测数据由小到大输出每一对符合条件的数。如果找不到符合条件的数对,则输出There are no solutions for N.。
每笔测试数据间请空一列。
Sample Input
61
62
0
Sample Output
There are no solutions for 61.
79546 / 01283 = 62
94736 / 01528 = 62
abcde / fghijk = N
这里每个英文字母代表不同的数字,第一个数字可以为0。
Input
输入包含许多笔待测数据,每列代表一笔待测数据,每笔待测数据包含一个正整数N,N为0时代表输入结束。
Output
对每笔待测数据由小到大输出每一对符合条件的数。如果找不到符合条件的数对,则输出There are no solutions for N.。
每笔测试数据间请空一列。
Sample Input
61
62
0
Sample Output
There are no solutions for 61.
79546 / 01283 = 62
94736 / 01528 = 62
暴力法简单枚举
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int judge(int a,int b)
{
if(a>98765) return 0;
int s[10]={0},sum=0;
if(b<10000) //小于10000,则补零
{
s[0]=1;
}
while(a)
{
s[a%10]=1;
a/=10;
}
while(b)
{
s[b%10]=1;
b/=10;
}
for(int i=0;i<10;i++) //十个数必须都出现且不重复
{
sum+=s[i];
}
if(sum==10)return 1;
else return 0;
}
int main(){
int ans=0,n,T=0;
while(scanf("%d",&n)&&n>=2&&n<=79)
{
for(int i=1234;i<=98765;i++)
{
if(judge(n*i,i))
{
printf("%05d / %05d = %d",n*i,i,n);
ans=1;
printf("\n");
}
}
if(!ans)
{
printf("no aswer!");
printf("\n");
}
T++;
}
return 0;
}