20701 Division |
| Difficulty level: B; programming language: Unlimited; run time limit: 1000ms; run space limit: 51200KB; code length limit: 2000000B |
| Question Description |
| Enter positive integer n to output all ABCDE and fghij that satisfy the expression abcde/fghij=n in order from small to large, where a~j happens to be an arrangement of the number 0~9. If the number of test instructions is not met, the output is 0. The five-digit number mentioned in the subject can include a leading 0, such as 01234, which is also called a five-digit number. |
| Input |
| A positive integer n |
| Output |
| Several rows, each of which consists of two qualifying five-bit positive integers (two of each row are first large and small), and two numbers are separated by a space. |
| Input example |
| 62 |
| Output example |
79546 01283 94736 01528 |
| Other Notes |
| Data range 2≤n≤79; from small to Osashi in the title, the first number of each row of output data is the order of small to large. |
First to get this problem, the first thought must be a mob search. That is to say, two numbers are to be cited for verification? Of course not, we can find another number if we just cite a number. So we got a beginner code:
#include <cstdio>intn,x,y;intMain () {scanf ("%d",&N); intf,g,h,i,j; for(intA=0; a<=9; a++) { for(intb=0; b<=9; b++) {b+ = (b==a); if(b>9) Break; for(intC=0; c<=9; C + +) {C+ = (c==a); C+ = (c==b); if(c>9) Break; for(intD=0;d <=9;d + +) {D+ = (d==a); D+ = (d==C); D+ = (d==c); if(d>9) Break; for(intE=0; e<=9; e++) {e+ = (e==a); E+ = (e==b); E+ = (e==c); E+ = (e==d); if(e>9) Break; for(intu=1; u<=1; u++) {x=a*10000+b* ++c* -+d*Ten+e; Y=x*N; if(y>99999) Break; //printf ("%d%d", x, y);f=y/10000; G= (y%10000)/ +; H= (y% +)/ -; I= (y% -)/Ten; J=y%Ten; if(F==g| | f==h| | f==i| | F==J) Break; if(g==h| | g==i| | G==J) Break; if(h==i| | H==J) Break; if(I==J) Break; if(f==a| | f==b| | f==c| | f==d| | F==E) Break; if(g==a| | g==b| | g==c| | g==d| | G==E) Break; if(h==a| | h==b| | h==c| | h==d| | H==E) Break; if(i==a| | i==b| | i==c| | i==d| | I==E) Break; if(j==a| | j==b| | j==c| | j==d| | J==E) Break; Elseprintf"%d%d%d%d%d%d\n", y,a,b,c,d,e); } } } } } } //System ("pause");}
However, when you are excited to submit it, you will find that either the result is wrong or the time overrun. This is because the program efficiency is too low, we might as well in a different way to enumerate, only with a loop, you can enumerate all the numbers, time complexity will be greatly reduced.
#include <iostream>Long Longn,x,y;BOOLt[9];into=0;intMain () {scanf ("%lld",&N); Long Longf,g,h,i,j,a,b,c,d,e; for(x=1234;x<99999; x + +) { for(intu=1; u<=1; u++) {f=x/10000; G= (x10000)/ +; H= (x +)/ -; I= (x -)/Ten; J=x%Ten; if(F==g| | f==h| | f==i| | F==J) Break; if(g==h| | g==i| | G==J) Break; if(h==i| | H==J) Break; if(I==J) Break; Y=x*N; if(y>=99999) Break; A=y/10000; b= (y%10000)/ +; C= (y% +)/ -; D= (y% -)/Ten; E=y%Ten; if(a==b| | a==c| | a==d| | A==E) Break; if(b==c| | b==d| | B==E) Break; if(c==d| | C==E) Break; if(d==e) Break; for(intL=0; l<=9; l++) t[l]=0; T[a]=1; t[b]=1; t[c]=1; t[d]=1; t[e]=1; t[f]=1; t[g]=1; t[h]=1; t[i]=1; t[j]=1; for(intL=0; l<=9; l++)if(!t[l])GotoLab; o++; printf ("%lld%lld%lld%lld%lld%lld%lld%lld%lld%lld\n", a,b,c,d,e,f,g,h,i,j);
Lab:intp; } } if(!o) printf ("0"); //System ("pause");}
Cnuoj 0703 20701 Division