Home > Back-end > Sequence number of the first n ugly, write code to run time is too long, want to know how to solve
Sequence number of the first n ugly, write code to run time is too long, want to know how to solve
Time:09-24
Title description If a number of factor contains only 2,3,5 or 7, so we call this number is called ugly, sequence 1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27... Shows the number of top 20 ugly, Please programming to find the first n elements in the sequence, Enter Input contains multiple sets of test data, each group of input is an integer n (1 & lt;=n<=5842), when n=0, input end, O For each group of input, The output line "The NTH humble number is The number.", The inside of The n by The input of The value of n to replace, "st", "nd", "rd" and "th" The usage with reference to The sample output at The end of The ordinal number, The sample input 1 2 3 4 11. 12 13 21 22 23 100 1000 5842 0 Sample output The 1 st humble number is 1. The 2 nd humble number is 2. The 3 rd humble number is 3. The 4 th humble number is 4. The 11 th humble number is 12. The 12 th humble number is 14. The 13 th humble number is 15. The 21 st humble number is 28. The 22 nd humble number is 30. The 23 rd humble number is 32. The 100th humble number is 450. The 1000th humble number is 385875. The 5842 nd humble number is 2000000000.
Write the code
# include & lt; Iostream> using namespace std; Bool humble (int n); Int main () { Int I, j=0, k=0, count=0; Int a, [10000]. Int s [10000]; for(i=0; I<=5842; I++) { Cin> A, [I]. If (a [I]==0) break; K++; } for(i=1; I<=2000000000; I++) { If (greet (I)==true) { S [j++]=I; count++; } } for(i=0; I{ If (j=a [I]) { If (j % 10==1 & amp; & J % 100!=11) Cout<" The "& lt; Else if (j % 10==2 & amp; & J % 100!=12) Cout<" The "& lt; Else if (j % 10==3 & amp; & J % 100!=13) Cout<" The "& lt; The else Cout<" The "& lt; } Cout<" The company number is "& lt; } return 0; } Bool humble (int n) { Int I, j, num=0; Int a, [10000]. Int b [10000]; Bool flag; If (n==1) Flag=1; for(i=2; I * i<=n; I++)//QiuZhi factor { If I (n %==0) A [num++]=I; While (n % I==0) N=n/I; } If (n> 1) A [num++]=n; For (j=0; J{ If (a [j]==2 | 3 | | a [j]==| a [j]==5 | | a [j]==7) Flag=1; The else Flag=0; } return flag; }
CodePudding user response:
Should be algorithm needs to be improved
CodePudding user response:
How do I look not to understand your topic. If his prime factors are 2 hc-positie, 1 what is the prime factors of it?
CodePudding user response:
Divided by 2 in a row, until cannot be divided exactly by one step, the results to the above steps five,7,3 test, if the result is 1 is ugly, or not, if you can't be divided exactly by 2 hc-positie any are not In addition, should build a table cache before calculated ugly for