Description
输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j各代表0~9中的一个数字 除了0可以重复外,其它数字不能重复,2<=n<=90000。
输入格式
多case,每行一个数字,最后一个数字是0
输出格式
除了最后一行0不用处理, 其它每个case,按被除数由小到大输出所有满足等式的情况 注:如果没有满足条件的等式,该case结束后,也需要输出一个空行 两个case之间用一个空行分隔
输入样例
666 6666 20000 0
27306/00041=666 41958/00063=666 43290/00065=666 52614/00079=666 53946/00081=666 63270/00095=666 20000/00001=20000 40000/00002=20000 60000/00003=20000 80000/00004=20000
提示
提示:6666没有找到满足条件的等式
思路
先遍历i找到符合的i、a;
再放到字符串内;
再通过题目要求检查字符串;
符合条件输出
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
while(true)
{
int n;
cin>>n;
if(n==0)
return 0;
for(int i=1;;i++)
{
int a=i*n;
if(a>99999)
break;
bool flag=true;
int arr[10];
memset(arr,0,sizeof(arr));//所有元素初始化为0
string str1=to_string(a);//将a和i转化为字符串
string str2=to_string(i);
for(char ch:str1)//遍历str1每个字符ch
{
arr[ch-'0']++;//将字符ch转化成对应数字,并在arr数组中记录出现次数
if(ch>'0'&&arr[ch-'0']>1)//ch不为0同时出现次数大于1次
flag=false;
}
for(char ch:str2)//遍历str2每个字符ch
{
arr[ch-'0']++;
if(ch>'0'&&arr[ch-'0']>1)
flag=false;
}
if(flag)
printf("%05d/%05d=%d\n",a,i,n);
}
cout<<endl;
}
return 0;
}