UVA725 - 725 - Division

来源:互联网 发布:台式电脑连接不上网络 编辑:程序博客网 时间:2024/06/03 21:43
Division
Time limit: 3.000 seconds

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$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 forN.". 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
一个简单的枚举题目,枚举0~9的所有排列时没有必要的!而且也是不可实现的!只需枚举题目中的 fghij 就可以算出 abcde, 然后再判断所有数字都不相同即可!
#include <stdio.h>#include <iostream>#include <string.h>#include <algorithm>using namespace std;int solve(int a, int b){    int bj[10], num[10], aa = a, bb = b; /// bj数组用来记录每一位出现的次数,num数组用来记录是否出现    memset(bj,0,sizeof(bj));    memset(num,0,sizeof(num));    if(b > 98765) return 0;    if(a < 10000) {num[0] = 1;bj[0] = 1;}///小于10000,存在一个前导0    while(a)    {        num[a%10] = 1;        bj[aa%10]++;        a /= 10;        aa /= 10;    }    while(b)    {        num[b%10] = 1;        bj[bb%10]++;        b /= 10;        bb /= 10;    }    int sum = 0;    for(int j = 0; j < 10; j++)        if(bj[j] == 1)            sum += num[j];    return (sum == 10);}int main(){    int n, t = 0;    while(scanf("%d",&n)&&n!=0)    {        if(t++)            printf("\n");        int cnt = 0;        for(int i = 1234; i < 100000; i++)        {            if(solve(i,i*n))            {                printf("%05d / %05d = %d\n",i*n,i,n);                cnt++;            }        }        if(!cnt)            printf("There are no solutions for %d.\n",n);    }    return 0;}



0 0
原创粉丝点击