UVa 725 DIVISION 除法

题目描述:

   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;        //虽然比较长,可是看起来还是挺可爱的
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值