UVA-725 Division-division

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.