uva725(除法)

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 . 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~j恰好为数字0~9的一个排列(可以有0前导)
思路:
枚举fghij就可以算出abcde,然后判断符不符合条件。
代码:
#include<cstdio>
using namespace std;
int main()
{
    int i,j,k,l,m,a,b;
    int b1,b2,b3,b4,b5;
    int flag;
    int N;
    int casex=0;
    while(scanf("%d",&N)&&N)
    {
        if(casex++)   printf("\n");
        flag=0;
      for( i=0;i<=9;i++)
          for(j=0;j<=9;j++)
            for(k=0;k<=9;k++)
                for(l=0;l<=9;l++)
                    for(m=0;m<=9;m++)
    {
        if(i!=j&&i!=k&&i!=l&&i!=m&&j!=k&&j!=l&&j!=m&&k!=l&&k!=m&&l!=m)
            a=i*10000+j*1000+k*100+l*10+m;
            else continue;
         b=N*a;
         if(b>99999)
            continue;
        b1=b/10000;
        b2=b%10000/1000;
        b3=b%10000%1000/100;
        b4=b%10000%1000%100/10;
        b5=b%10;
        if(b1!=b2&&b1!=b3&&b1!=b4&&b1!=b5&&b2!=b3&&b2!=b4&&b2!=b5&&b3!=b4&&b3!=b5&&b4!=b5&&b1!=i&&b1!=j&&b1!=k&&b1!=l&&b1!=m&&b2!=i&&b2!=j&&b2!=k&&b2!=l&&b2!=m&&b3!=i&&b3!=j&&b3!=k&&b3!=l&&b3!=m&&b4!=i&&b4!=j&&b4!=k&&b4!=l&&b4!=m&&b5!=i&&b5!=j&&b5!=k&&b5!=l&&b5!=m)
        {
          printf("%d / %d%d%d%d%d = %d\n",b,i,j,k,l,m,N);
          flag=1;
        }
        else
            continue;

       }

    if(!flag)  printf("There are no solutions for %d.\n",N);

    }
    return 0;

}

 

Miguel Revilla

2000-08-31

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-21 15:04:47

uva725(除法)的相关文章

Uva725 除法

Uva725 除法 题目描述: 输入正整数n,按从小到大的顺序输出所有形如\(abcde/fghij=n\)的表达式,其中\(a-j\)恰好为数字0-9的一个排列(可以有前导0). 思路: 枚举\(fghij\),然后计算出\(abcde\),看所有的数字是否重复.这里主要是要注意判断0-9数字分别出现一次时的效率问题. 代码实现: #include <iostream> #include <map> #include <algorithm> #define LL lo

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

7_1 除法(UVa725)&lt;选择合适的枚举对象&gt;

如果把数字0到9分配成2个整数(各五位数),现在请你写一支程序找出所有的配对使得第一个数可以整除第二个数,而且商为N(2<=N<=79),也就是:abcde / fghijk = N这里每个英文字母代表不同的数字,第一个数字可以为0.Input输入包含许多笔待测数据,每列代表一笔待测数据,每笔待测数据包含一个正整数N,N为0时代表输入结束.Output对每笔待测数据由小到大输出每一对符合条件的数.如果找不到符合条件的数对,则输出There are no solutions for N..每笔测

例题7-1 除法 UVa725

1.题目描述:点击打开链接 2.解题思路:本题利用暴力搜索解决.直接从1234开始枚举除数,由于乘积不能超过100000,所以上限是100000/n.然后判断得到的乘积和除数中的所有数是否都各不相同即可. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<set&

shell中除法运算

A=`expr $num1 / $num2` 这个时候num3=0 ,是因为是因为expr不支持浮点除法 小数点标识的方法: A=`echo "scale=2; $num1/$num2" | bc` 使用bc工具,sclae控制小数点后保留几位 另一种方法 A=awk 'BEGIN{printf "%.2f\n",'$num1'/'$num2'}' 百分比表示 A=awk 'BEGIN{printf "%.2f%\n",('$num1'/'$nu

计算机中如何实现除数是2的幂次的除法【转载自CSDN】

前言: 本来是在看汇编里面的数据条件传送指令,做习题的时候看着这么一道有关于2的幂次方除法的题目.结果傻眼了,又尼玛不会了.........第二章看的时候就稀里糊涂的,看了几遍也没看太懂,这回又涉及到了 ,发现再回来看还是容易一点.所以写此博文,方便日后复习. 我今天遇到的问题如下: 问题: 除法,在我们平时的算数运算中,结果总是向0的方向舍入的,但是在计算机中,舍入的方式有所不同.在大多数的机器中,除法要比乘法还有加法这些运算都要慢很多倍,计算机中对于2的幂次这种数很是敏感,因为计算机当中用到

【练习赛2补题】poj 2325 Persistent Numbers 【高精度除法+贪心】

Description The multiplicative persistence of a number is defined by Neil Sloane (Neil J.A. Sloane in The Persistence of a Number published in Journal of Recreational Mathematics 6, 1973, pp. 97-98., 1973) as the number of steps to reach a one-digit

一个基础而奇怪的问题:算法执行加法、乘法、除法性能无区别?

一个基础而奇怪的问题:算法执行加法.乘法.除法性能无区别? 计算机原理分析觉得:加法.乘法和除法的计算性能依次减少,但减少到什么程度? 编写C程序用30次百万数据计算来測试时间差异性,代码例如以下: #include <stdio.h> #include <stdlib.h> #include <time.h> #define N 1000000 void add(float x[], long n) { float sum = 0; for(long i = 0; i

大数的减法除法

1.除法 1 //反着除 2 //c==0; 3 for(j=len;j>=1;j--) 4 { 5 s=a[j]+c*10; 6 a[j]=s/(i+1); 7 c=s%(i+1); 8 } 9 10 //排除前面的0 11 while(a[i][len]==0) 12 len--; 13 a[0]=len; 2.减法 跟除法的方式类似. 略.