UVa725 Division(暴力)

来源:互联网 发布:国泰君安富易交易软件 编辑:程序博客网 时间:2024/06/05 06:23

UVa725 Division (暴力)

Description
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
———— = N
fghij
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

题意:求0-9组成的两个五位数/四位数【每个数只能用一次】,即 xxxxx / xxxxx = N 的种类,暴力打表出01234-98765所以的解,然后处理。

#include <algorithm>#include <iostream>#include <cstring>#include <cstdio>#include <ctime>#include <set>#include <map>#include <cmath>using namespace std;#define pi 3.14159265359typedef long long LL;const int maxn = 100;int n, h, k;int a[maxn];int su[100][maxn],di[100][maxn],num[20];//分子、分母、记录0-9使用的次数void fun(){    int flag,sum,temp,cnt;    int div = 01234;    memset(su,0,sizeof(su));    memset(di,0,sizeof(di));    for(int i=2;i<=79;i++){        cnt = 1;        for(int j=01234;j<=98765;j++){            sum = i*j;                  if(sum>98765 || sum<999 ) break;            div = j;            flag = 1;            memset(num,0,sizeof(num));            int cou=5;            while(cou--){//看成五位数,循环5次                temp = sum%10;                num[temp]++;                if(num[temp]>1){                    flag = 0;                    break;                }                 sum/=10;                temp = div%10;                num[temp]++;                if(num[temp]>1){                    flag = 0;                    break;                }                 div/=10;                    }                       if(flag){                su[i][cnt] = j;//分子                di[i][cnt++] = i*j;//分母            }        }        a[i] = cnt;//存放i个数的解    }}int main(){    fun();    int flag_ = 1;    while(~scanf("%d",&n)&&n){        if(flag_){//格式            flag_ = 0;          }else{            printf("\n");        }        if(a[n]==1){            printf("There are no solutions for %d.\n",n);               continue;        }         for(int i=1;i<a[n];i++){//          cout<<su[n][i]<<" "<<di[n][i]<<endl;            if(su[n][i]<999) continue;            printf("%05d / %05d = %d\n",di[n][i],su[n][i],n);        }    }    return 0;}
0 0
原创粉丝点击