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.
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.
61 62 0
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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/a1967919189/article/details/47089811