天天看点

Division,Uva725

暴力求解Description

Download as PDF

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

Sample Output

There are no solutions for 61.

79546 / 01283 = 62

94736 / 01528 = 62

解题思路:枚举其中的分母或分子,这样能提高效率,在判断数字是否在0~9之间,用数组标法,做过好多题都是用数组标记法来判断该数字是否出现过,

还有就是注意细节。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int N,f=;
int meiju(int a,long long b);
int main()
{
    int k=;
    while(cin>>N&&N!=)
    {    

        if(k!=)       //细节所在,
            cout<<endl;  
        k++;
      for(int i=;i<;i++)
        {
            if((i/N)*N==i)
            {
                 if(meiju(i,i/N))
                 {
                     printf("%05d / %05d = %d\n",i,i/N,N);
                     f=;
                 }
            }
        }
        if(f==)
         printf("There are no solutions for %d.\n",N);
            f=; 
    }
 return ;

}
int meiju(int a,long long b)
{
     int a1,b1;
     int A[]={};
     a1=a,b1=b;
    while(a1)
    {
        if(A[a1%])  //标记分母中出现的数字。
            return ;

        else 
            A[a1%]=;
         a1=a1/;
    }
    while(b1)
    {
        if(A[b1%])  //标记分子中出现的数字。
            return ;
        else 
            A[b1%]=;
            b1=b1/;
    }
   for(int i=;i<;i++)
       if(A[i]!=)return ;
       return ;
}