UVa725 Division(暴力)

11 篇文章 0 订阅
2 篇文章 0 订阅

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.14159265359
typedef 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值