Analysis: All permutations of enumeration 0-9? No need, just enumerate fghij to calculate ABCDE (=FGHIJ * n), and then determine if all the numbers are different. Not only the program is simple, but also the enumeration amount from 10! =3628800 is reduced to less than 10,000, and when the number of ABCDE is not equal to 5, it is possible to terminate the enumeration (remember that n is greater than or equal to 2 yo!). )
The AC code is as follows: Time is 1573MS.
#include <cstdio> #include <cstring> #include <iostream> #include <cmath>using namespace std;// N>=2int vis[10],n,num;//Whether each number is used, the number to be entered, the number of bits to divide! The divisor is obtained by dividend and quotient! int a[5],have;int getnum (int cnt)//Gets the value represented by the number of columns stored in the current a array! {int sum=0,s=0; for (int i=cnt-1; i>=0; i--) sum+= (int) pow (10,s++) *a[i]; return sum;} BOOL judge (int s)//determine if s meet the requirements! {num=0; int vis1[10]; for (int i=0;i<10;i++) vis1[i]=vis[i];//The status of the Vis to vis1, lest the search error! while (s) {if (!vis1[s%10])//means s%10 This number has not appeared! Vis1[s%10]=1; else return false; s/=10; num++; The IF (num==5)//num can only be equal to 5, possibly equal to other numbers! return true; return false;//because NUM is not equal to 5, indicating that it does not meet the requirements! }void Fun (int cnt) {if (cnt>=5) {int chu=getnum (CNT) *n; if (judge (Chu)) {have=true; cout<<chu<< "/"; for (int ii=0; ii<cnt; ii++) cout<<a[ii]; cout<< "=" <<n<<endl; } return; } for (int i=0;i<10;i++) {if (!vis[i]) {vis[i]=1; A[cnt]=i; Fun (cnt+1); vis[i]=0; }}}int Main () {int x=1; while (cin>>n,n) {have=false; memset (vis,0,sizeof (VIS)); if (x>1) cout<<endl; x + +; Fun (0); if (!have) cout<< "There is no solutions for" <<n<< '. ' <<endl; } return 0;}
UVA-725 Division-division