UVA725 Division【枚举】

 
Division

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

. 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 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

题目大意:输入一个正整数N,要求从小到大的输出形如abcde / fghij = n的表达式。

要求a~f为数字0~9,且不能重复(前边可有0)。

思路:如果将a b c d e f g h i j全部遍历的话,复杂度是10!,没有必要,直接枚举

f g h i j,然后算出a b c d e,去判断是否存在重复数字即可。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int sum,num,N,j;
    int a,b,c,d,e;
    int f,g,h,l,m;
    int x[10],ok = 0;
    while(~scanf("%d",&N) && N)
    {
        if(ok == 0)
            ok = 1;
        else
            printf("\n");
        sum = 0;
        for(int i = 1234; i <= 98765; i++)
        {
            memset(x,0,sizeof(x));
            a = i%10,b = i/10%10,c = i/100%10,d = i/1000%10,e = i/10000%10;
            num = i*N;
            if(num>98765)
                break;
            f = num%10,g = num/10%10,h = num/100%10,l = num/1000%10,m = num/10000%10;
            x[a]++,x[b]++,x[c]++,x[d]++,x[e]++;
            x[f]++,x[g]++,x[h]++,x[l]++,x[m]++;
            for(j = 0; j <= 9; j++)
                if(x[j]>1)
                    break;
            if(j==10)
            {
                printf("%d%d%d%d%d / %d%d%d%d%d = %d\n",m,l,h,g,f,e,d,c,b,a,N);
                sum++;
            }
            else
                continue;
        }
        if(sum==0)
            printf("There are no solutions for %d.\n",N);
    }

    return 0;
}
时间: 2024-09-29 16:07:50

UVA725 Division【枚举】的相关文章

uva725 Division

题目描述: abcde / fghij =N a,b···j 为0-9任意一个数,且互相不同 任意给一个n(2<=n<=79),输出满足条件的可能 思路1:10!只有不到400w,直接暴力枚举即可 #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int a[]= {1,0,2,3,4,5,6,7,8

UVA725 UVALive5362 Division

Regionals 1990 >> North America - East Central NA 这可以说是最快枚举程序,比网上现有的暴力枚举程序要快. 问题链接:UVA725 UVALive5362 Division.基础练习题,用C语言编写程序. 题意简述:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果.如果没有找到则输出"There are no solutions for N."

UVa 725 Division --- 简单枚举

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666 /* UVa 725 Division --- 简单枚举 */ #include <cstdio> #include <cstring> bool used[10]; /* 判断传进来的两个数是否满足条件 */ bool judge(int a, i

UVA 725 Division ( 找出 abcde / fghij = N的形式—— 暴力枚举 )

Division Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu 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 divide

uva 725 Division(暴力枚举)

uva 725  Division 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 . That is, abcde / fghij =

UVa 725 - Division(枚举简单题)

今日无事,写篇日记.这是我写的第一道uva题(uva是算法竞赛入门经典里使用例题的题目来源),正值暑假,前几天看了书上中文题目和解析,玩了几日san11,又重拾起acm,今天晚上写了一下还是花了点时间. 题目:给你一个数字n,用0~9,10个数字组成两个五位数,使得他们的商为n,按顺序输出所有结果. 1.这里的除法不是c中的除(/)而是整除,一开始以为是整除,但那样79546 / 01283 = 62是一组解,79546+1 / 01283 = 62也是一组解了. 2.复杂度:枚举分子然后分子乘

枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)

1 //Uva725 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 using namespace std; 7 8 void evalu(int n) 9 { 10 const int maxn = 1024 + 10; 11 char num[10]; //将数字用字符保存 12 int flag[10]; //判断每个数,是否重

UVA 725 Division(hash+枚举)

题目大意: 就是说给你一个abcde/fghij = n的表达式,给你一个n,让你求出有多少个这样的式子. 解题思路: 最为简单的枚举了,要注意到我们只需要枚举出来fghij就可以了,因为abcde=fghij*n,这样的话,时间复杂度由10!就 降低到了5!,在枚举结束后,我们只需要判断0-9这些数字是否都出现了且仅出现一次即可.然后对于超过5位的数字,我们 直接break掉. 这道题的输出一定要注意那个前导0,,一开始忘记了,WA了两次,然后果断用setw(5)和setfill('0')给搞

UVA-725除法-Division

分析:  枚举0-9的所有排列?没这个必要,只需要枚举fghij就可以计算出abcde(=fghij * n),然后判断是否所有的数字都不相同即可.不仅程序简单,而且枚举量也从10!=3628800降低至不到1万,而且当abcde的位数不等于5的时候,就可以终止枚举了(记住n是大于等于2的哟!) AC代码如下:用时为1573MS. #include<cstdio> #include<cstring> #include<iostream> #include<cmat