#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char buf[99];
int main(){
int kase=0,n;
while(cin>>n&&n){
int cnt=0;
if(kase++) cout<<endl;
for(int fghij=1234;;fghij++){//fghij=1234即01234 這裏沒有按排列順序枚舉 而是採用生成測試法(即從01234到99999每次自增1 ) 更簡單
int abcde=fghij*n; //fghij枚舉之後 再根據關係式得到abcde 利用sprintf() 合併成一個字符串 再判斷是否是一個排列
sprintf(buf,"%05d%05d",abcde,fghij);//%05d 0的作用是位數不足則補充0
if(strlen(buf)>10) break;//位數超過10 肯定不成立
sort(buf,buf+10);
//判斷是否爲一個排列
bool ok=true;
for(int i=0;i<10;i++){
if(buf[i]!='0'+i){
ok=false;
break;
}
}
if(ok){
cnt++;
printf("%05d / %05d = %d\n",abcde,fghij,n);
}
}
if(!cnt) printf("There are no solutions for %d.\n",n);
}
return 0;
}
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
char buf[99];
int main(){
int kase=0,n;
while(cin>>n&&n){
int cnt=0;
if(kase++) cout<<endl;
for(int fghij=1234;;fghij++){//fghij=1234即01234 這裏沒有按排列順序枚舉 而是採用生成測試法(即從01234到99999每次自增1 ) 更簡單
int abcde=fghij*n; //fghij枚舉之後 再根據關係式得到abcde 利用sprintf() 合併成一個字符串 再判斷是否是一個排列
sprintf(buf,"%05d%05d",abcde,fghij);//%05d 0的作用是位數不足則補充0
if(strlen(buf)>10) break;//位數超過10 肯定不成立
//sort(buf,buf+10);
//判斷是否爲一個排列
bool ok=true;
set<char> s;//用set判斷是否有重複元素 即是否是一個排列
s.clear();
for(int i=0;i<10;i++){
if(s.count(buf[i])){
ok=false;
break;
}
else s.insert(buf[i]);
}
if(ok){
cnt++;
printf("%05d / %05d = %d\n",abcde,fghij,n);
}
}
if(!cnt) printf("There are no solutions for %d.\n",n);
}
return 0;
}
第二個代碼利用set判重