Division,Uva725

来源:互联网 发布:mac os 最快稳定的版本 编辑:程序博客网 时间:2024/06/07 02:26

暴力求解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 2N79. 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

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

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int N,f=0;int meiju(int a,long long b);int main(){    int k=0;    while(cin>>N&&N!=0)    {            if(k!=0)       //细节所在,            cout<<endl;          k++;      for(int i=10000;i<98765;i++)        {            if((i/N)*N==i)            {                 if(meiju(i,i/N))                 {                     printf("%05d / %05d = %d\n",i,i/N,N);                     f=1;                 }            }        }        if(f==0)         printf("There are no solutions for %d.\n",N);            f=0;     } return 0;}int meiju(int a,long long b){     int a1,b1;     int A[10]={0};     a1=a,b1=b;    while(a1)    {        if(A[a1%10])  //标记分母中出现的数字。            return 0;        else             A[a1%10]=1;         a1=a1/10;    }    while(b1)    {        if(A[b1%10])  //标记分子中出现的数字。            return 0;        else             A[b1%10]=1;            b1=b1/10;    }   for(int i=1;i<10;i++)       if(A[i]!=1)return 0;       return 1;} 
0 0
原创粉丝点击