UVa 725 DIVISION 除法

来源:互联网 发布:卖家淘宝小二怎么联系 编辑:程序博客网 时间:2024/06/05 03:48

题目描述:

   Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0through 9 once each, such that the first number divided by the second is equal to an integer N, where2 ≤ N ≤ 79. That is,

abcde / fghij = N

   where each letter represents a different digit. The first digit of one of the numerals is allowed to bezero.

Input:

Each line of the input file consists of a valid integer N. An input of zero is to terminate the program.


Output:

Your program have to display ALL qualifying pairs of numerals, sorted by increasing numerator (and,of course, denominator).Your output should be in the following general form:

xxxxx / xxxxx = N

xxxxx / xxxxx = N..

In case there are no pairs of numerals satisfying the condition, you must write ‘There are nosolutions for N.’. Separate the output for two different values of N by a blank line.

Sample Input 

61

62

0

Sample Output:

There are no solutions for 61.

79546 / 01283 = 62

94736 / 01528 = 62



一道枚举题,又由于每个数字的不能重复,启发我用回溯写了一个全排列。然后先求fghij,靠fghij和n的乘积来求abcde以此来减小枚举量。

不过因为没有看清楚输出的格式让我错了好几次,怪难受的。

#include <cstdio>#include <iostream>#include <cstring>using namespace std;int n;void print(long int y,long int x);bool pd(long int x);bool fd[11]={0};bool fdd[11]={0};long int sum=0;int ff=0;void hui(int x)     //回溯,来求每一种可能。{    if(x<=5)    {        for(int i=0;i<=9;i++)            if(fd[i]==0)            {                sum=sum*10+i;                fd[i]=1;                     hui(x+1);                     fd[i]=0;                sum=(sum-i)/10;              }    }    else    {        long int y=sum*n;               if(pd(y))        {            print(y,sum);        }        else return;    }}bool pd(long int y){    for(int i=0;i<=10;i++)fdd[i]=fd[i];    if(y<1234||y>98765)return 0;    else    {        int t=5,f=1;        while(t--)        {            int z=y%10;            y=y/10;            if(fdd[z]==1){f=0;break;}            fdd[z]=1;        }        if(f==1)return 1;        else return 0;    }}void print(long int y,long int x){        if(y<10000)printf("0%ld /",y);        else printf("%ld /",y);        if(x<10000)printf(" 0%ld = %d\n",x,n);        else printf(" %ld = %d\n",x,n);        ff=1;}int main(){    int fz=0;    while(cin>>n&&n)    {        ff=0;        if(fz==1)cout<<endl;        int y;        memset(fd,0,sizeof(fd));        hui(1);        if(ff==0)printf("There are no solutions for %d.\n",n);        fz=1;    }    return 0;        //虽然比较长,可是看起来还是挺可爱的}