Division(简单枚举)

  Division 
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 . 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<stdio.h>
#include<string.h>
int main()
{
    int n,i,j,k,l,t,o,p,ans,h;
    int b[10]= {0};
    int lu[10]= {0};
    int pp=0;
    while(~scanf("%d",&n)&&n)
    {
        int kk=0;

        if(pp)printf("\n");///除了第一个n之外,每输出一个n后面都要输出一行空格;
        pp++;

        for(i=0; i<=9; i++) ///找第一个数;
        {
            ans=i;

            for(j=0; j<=9; j++) ///找第二个数;
            {
                if(j==i)continue;///不能和已找到的数相同;
                ans=ans*10+j;

                for(k=0; k<=9; k++) ///找第三个数;
                {
                    if(k==i||k==j)continue;///不能和已找到的数相同;
                    ans=ans*10+k;

                    for(l=0; l<=9; l++) ///找第四个数;
                    {
                        if(l==i||l==j||l==k)continue;///不能和已找到的数相同;
                        ans=ans*10+l;

                        for(t=0; t<=9; t++) ///找第五个数;
                        {
                            if(t==i||t==j||t==k||t==l)continue;///不能和已找到的数相同;
                            ans=ans*10+t;

                            if(ans%n==0)///如果是n的倍数就观察两数之商是否符合条件;
                            {
                                p=ans/n;
                                int f=0;
                                for(h=0; h<5; h++)
                                {
                                    o=p%10;
                                    if(o==i||o==l||o==j||o==k||o==t||lu[o])///不能和已找到的数相同;
                                    {
                                        f=1;///不符合条件将lu和b初始化;
                                        memset(lu,0,sizeof(lu));
                                        memset(b,0,sizeof(b));
                                        break;
                                    }
                                    lu[o]=1;
                                    p=p/10;
                                    b[h]=o;
                                }
                                if(f)
                                {
                                    ans/=10;///返回上个ans值;
                                    continue;
                                }
                                else///符合条件输出,别忘了初始化;
                                {
                                    kk=1;
                                    printf("%d%d%d%d%d / %d%d%d%d%d = %d\n",i,j,k,l,t,b[4],b[3],b[2],b[1],b[0],n);
                                    memset(lu,0,sizeof(lu));
                                    memset(b,0,sizeof(b));
                                }
                            }
                            ans/=10;///返回上个ans值;
                        }
                        ans/=10;///返回上个ans值;
                    }
                    ans/=10;///返回上个ans值;
                }
                ans/=10;///返回上个ans值;
            }
        }

        if(!kk)printf("There are no solutions for %d.\n",n);///没有答案时;
    }
}

///遍历最小值到最大值;
#include<stdio.h>
#include<string.h>
int main()
{
    int n,i,j,f;
    int pp=0,p,lu[16]={0},a[10]={0},b[10]={0};
    while(~scanf("%d",&n)&&n)
    {

        if(pp)printf("\n");
        pp=1;
        int ll=0;
        for(i=1234;i<=98765;i++)///从最小值到最大值;
        {
            if(i*n>98765)break;
            p=i*n;
            int k=i;
            int qq=0;
            for(j=0;j<5;j++)
            {
                a[j]=k%10;
                k/=10;
                if(lu[a[j]])///有重复值时不符合条件,初始化数组;
                {
                    qq=1;
                    memset(a,0,sizeof(a));
                    memset(b,0,sizeof(b));
                    memset(lu,0,sizeof(lu));
                    break;
                }
                lu[a[j]]=1;
            }
            if(qq)continue;
            f=0;
            for(j=0;j<5;j++)
            {
                b[j]=p%10;
                p/=10;
                if(lu[b[j]])///有重复值时不符合条件,初始化数组;
                {
                    f=1;
                    memset(a,0,sizeof(a));
                    memset(b,0,sizeof(b));
                    memset(lu,0,sizeof(lu));
                    break;
                }
                lu[b[j]]=1;
            }
            if(!f)///找到符合条件值时输出,初始化数组;
            {
                ll=1;
                printf("%d%d%d%d%d / %d%d%d%d%d = %d\n",b[4],b[3],b[2],b[1],b[0],a[4],a[3],a[2],a[1],a[0],n);
                memset(a,0,sizeof(a));
                memset(b,0,sizeof(b));
                memset(lu,0,sizeof(lu));
            }
        }
        if(!ll)printf("There are no solutions for %d.\n",n);///没有结果时;
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值