UVA 725 Division

来源:互联网 发布:非农就业数据公布 编辑:程序博客网 时间:2024/05/18 10:33

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<=N <=79. That is,abcde / fghij =Nwhere 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

题意

给出一个公式 abcde/fghij=n.输入n,输出abcde/fghij=n。当输入0时,循环结束;

代码实现

#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <cstring>using namespace std;int _list[10]; //存储abcdefghij的数值bool ok(int x, int y){    memset (_list, 0, sizeof (_list));//初始化为0    for (int i=1; i<=5; ++i)    {        _list[x%10]++;        _list[y%10]++;        if (_list[x%10] > 1 || _list[y%10] > 1)            return 0;        x /= 10;        y /= 10;    }     return true;} int main() {     int N, cnt = 0;     while (scanf ("%d", &N) == 1)     {         if (N == 0)            break;         if (cnt++)            puts ("");         int one = 0;        for (int i=1234; i<=100000/N; ++i)        {             if (i * N > 98765) //abcde最大值为98765                break;           if (ok (i, i*N) == true)//用ok()函数进行判断            {                 printf ("%05d / %05d = %d\n", N*i, i,N );                 one++;            }        }        if (!one)            printf ("There are no solutions for %d.\n", N);     }    return 0;}
0 0
原创粉丝点击