UVa_725 - Division

 Division 

Write a program that finds and displays all pairs of 5-digit numbers thatbetween them use the digits0 through 9 once each, such that thefirst number divided by the second is equal to an integerN, where $2\le N \le 79$.That is,


abcde / fghij = N

where each letter represents a different digit.  The first digit of one ofthe 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 terminatethe program.

Output 

Your program have to display ALL qualifying pairs of numerals, sorted byincreasing 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 mustwrite ``There are no solutions forN.". Separate the output for twodifferent 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

 

 

题意:

给定一个数n,判定是否存在a / b = n;其中a和b均为5位数,这十位数是0-9的一个排列

题解:

1.直接 暴力枚举即可,枚举b: 1234~98765, 那么a = b * n;

2. 此时判断组成a和b的十位数是否为0~9的一个排列即可(每个数字只出现一次,且不能重复)

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int used[10];
bool Judge(int a, int b) // a / b = N
{
    if(a > 98765) return false;
    for(int i = 0; i < 10; i++) used[i] = 0;
    if(b < 10000) used[0] = 1;
    while(b){
        used[b%10]++;
        b /= 10;
    }
    while(a){
        used[a%10]++;
        a /= 10;
    }
    for(int i = 0; i < 10 ; i++)
        if(used[i] != 1) return false;
    return true;
}

int main()
{
   int N, icase = 0;
   int a, b;
   while(cin>>N && N)
   {
       if(icase++) cout<<endl;
       int a, b;
       bool exist = false;
       for(b = 1234; b <= 98765; b++){
            a = b * N;
            if(Judge(a, b)) { exist = true; printf("%05d / %05d = %d\n", a, b, N); }
       }
       if(!exist) printf("There are no solutions for %d.\n", N);
   }
   return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值