Division

Description

Download as PDF
 

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

abcde / fghij =N

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

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 no solutions 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

 暴力解决,不过要注意输出时最后一组数据不能多出空行

#include"iostream"
#include"cstring"
using namespace std;

int n;
int book[100]= {0};

bool judge(int c,int cc,int ccc)
{
    memset(book,0,sizeof(book));
    if(ccc==0) book[0]++;
    int t,f;
    t=c;
    f=0;
    while(t/10>0)
    {
        book[t%10]++;
        if(book[t%10]>1)
        {
            f=1;
        }
        t/=10;
    }
    book[t]++;
    if(book[t]>1)
    {
        f=1;
    }
    t=cc;
    while(t/10>0)
    {
        book[t%10]++;
        if(book[t%10]>1)
        {
            f=1;
        }
        t/=10;
    }
    book[t]++;
    if(book[t]>1)
    {
        f=1;
    }
    if(f) return false;
    return true;
}




void go()
{

    int i,flag;
    flag=0;
    for(i=1111; i<=99999; i++)
    {
        if(i*n>98765) break;
        if(judge(i,i*n,1))
        {
            if((i*n)/10000==0) continue;
            if(i/10000==0&&judge(i,i*n,0))
            {
                cout<<i*n<<" / 0"<<i<<" = "<<n<<endl;
                flag=1;
            }
            if(judge(i,i*n,1)&&i/10000!=0)
            {
                cout<<i*n<<" / "<<i<<" = "<<n<<endl;
                flag=1;
            }

        }
    }
    if(flag==0) cout<<"There are no solutions for "<<n<<'.'<<endl;
}


int main()
{
    int x=1;
    while(cin>>n&&n)
    {
        if(x>1)   cout<<endl;
        x++;
        go();

    }
    return 0;
}

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注