首页 > 其他 > 详细

uva 725 - Division

时间:2015-01-23 18:30:14      阅读:318      评论:0      收藏:0      [点我收藏+]

  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, where技术分享. 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 <stdio.h>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
using namespace std;
int a[11],n;
int is(int i)
{
    if(a[i]==1)return 1;
    return 0;
}
int judge(int i,int j,int k,int o,int p)
{
    int x,s,s1,s2;
    memset(a,0,sizeof(a));
    a[i]++;a[j]++;a[k]++;a[o]++;a[p]++;
    s1=10000*i+1000*j+100*k+10*o+p;
    s=s1*n;
    s2=s;
    while(s){
        a[s%10]++;
        s/=10;
    }
    if(is(1)&&is(2)&&is(3)&&is(4)&&is(5)&&is(6)&&is(7)&&is(8)&&is(9)&&is(0))
        {printf("%d / %05d = %d\n",s2,s1,n);return 1;}
    else return 0;
}
int main()
{
    int i,j,k,o,p,t=0;
    while(scanf("%d",&n),n){
       t++;
       if(t!=1)puts("");
       int e=0;
       for(i=0; i<=9; i++)
       for(j=0; j<=9; j++)
       for(k=0; k<=9; k++)
       for(o=0; o<=9; o++)
       for(p=0; p<=9; p++)
            if(judge(i,j,k,o,p))
                e=1;
       if(!e)printf("There are no solutions for %d.\n",n);
    }
    return 0;
}


uva 725 - Division

原文:http://blog.csdn.net/u014705854/article/details/43057249

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!