标签:
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.
题目大意:输入N,要你在0~9这些数中选数组成2个5位数(每个都要用且不能重复),这2个数的商就是N,把它的所有解输出,没有则输出无。
题目思路:因为每个数都要用所以最小的数肯定是01234,最大大概取50000(N最小是2,50000*2超过5位数了)。用这个乘上N来求得除数,在判断这2个数满足要求(0~9每个数用一次),用一个长度为10的数组,用循环把这2个数每次对10取余,在除10,取余的作为数组的下标给这个位置赋值为1,循环完了,把数组0~9加起来,看是否等于10,等于输出这2个数,否则继续循环, 如果2个数的位数超过10位就直接退出循环。
(注意在输出案例的时候第一个案例不需要输出换行但后面都要输出换行)
代码:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 int a[15]; 6 int main() 7 { 8 int N,j,m,sum,b,c,con=0; 9 int i; 10 while(cin>>N&&N) 11 { 12 m=0; 13 if(con++) 14 printf("\n"); 15 for(i=1234;i<99999;i++) 16 { 17 memset(a,0,sizeof(a)); 18 if((i*N)/100000==0) 19 { 20 b=i; 21 for(j=0;j<5;j++) 22 { 23 a[b%10]=1; 24 b/=10; 25 } 26 c=i*N; 27 for(j=0;j<5;j++) 28 { 29 a[c%10]=1; 30 c/=10; 31 } 32 sum=0; 33 for(j=0;j<10;j++) 34 { 35 sum+=a[j]; 36 37 } 38 if(sum==10) 39 { 40 printf("%05d / %05d = %d\n",i*N,i,N); 41 m=1; 42 } 43 else continue; 44 45 } 46 else break; 47 } 48 if(m==0) 49 printf("There are no solutions for %d.\n",N); 50 } 51 return 0; 52 }
标签:
原文地址:http://www.cnblogs.com/huaxiangdehenji/p/4694104.html