数字不能重复,利用分子找分母.
#include<iostream> #include<cstring> #include<cmath> #include<set> using namespace std; int panduan(int x,int y,int n) { set<int>s; int a[11],i; for(i=0;i<5;i++) { a[i]=x%10; s.insert(a[i]); x/=10; } for(i=5;i<10;i++) { a[i]=y%10; s.insert(a[i]); y/=10; } if(s.size()==10) { for(i=9;i>=5;i--) cout<<a[i]; cout<<" /"; cout<<' '; for(i=4;i>=0;i--) cout<<a[i]; cout<<" ="; cout<<' '<<n<<endl; return 1; } return 0; } int main() { int n,i,sum,count=-1; while(cin>>n && n) { count++; sum=0; if(count) cout<<endl; for(i=1234;i<=98765;i++) { int m=n*i; if(m>98765) break; else { int c=panduan(i,m,n); if(c) sum++; } } if(sum==0) cout<<"There are no solutions for"<<' '<<n<<'.'<<endl; } return 0; }