Humble Numbers
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 18238 Accepted Submission (s): 7934
Problem Descriptiona number whose only prime factors be 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, ten,, +, (+), A, ten, A, ten, ten, ... shows the first humble numbers.
Write a program to find and print the nth element in this sequence
Inputthe input consists of one or more test cases. Each test case consists the one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for N.
Outputfor each test case, print one line saying "The nth humble number is number." Depending on the value of n, the correct suffix "st", "nd", "ts", or "th" for the ordinal number nth have to is used like I T is shown in the sample output.
Sample Input1234111213212223100100058420
Sample outputthe 1st Humble number is 1.The 2nd humble number is 2.The 3rd humble number are 3.The 4th humble number is 4.T He 11th humble number is 12.The 12th humble number is 14.The 13th humble number was 15.The 21st humble number is 28.The 22n D Humble number is 30.The 23rd humble number are 32.The 100th humble number is 450.The 1000th humble number is 385875.The 5 842nd humble number is 2000000000.
Said DP a bit far-fetched, but also is to use the state of the front to launch the back of the state, English knowledge is a easy wrong point.
Idea: 2,5,7,9 is ugly number, ugly number of 2,5,7,9 times is ugly number, so starting from 1, the calculation of each number of its 2,5,7,9 times, calculated the results again double. Inevitably encounter this kind of study other messy knowledge points, the output suffix is easy to mistake, in English if a number of bits is 1, then followed with st,2 and nd,3 with Rd, other with th, but note that the latter two is 11,12,13 except , These numbers are followed by th, such as 12,10013,1111, I wa three times, the consequences of poor English.
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 #defineMAX 58455 6 intMy_min (intAintBintCintd);7 intMainvoid)8 {9 intN;Ten intS[max] = {1}; One inti,i_2,i_3,i_5,i_7; A intbox,box_2,box_3,box_5,box_7; - Charcha[5]; -i = i_2 = I_3 = I_5 = I_7 =0; the - while(I < MAX-2) - { -Box_2 = s[i_2] *2; +Box_3 = s[i_3] *3; -Box_5 = s[i_5] *5; +box_7 = s[i_7] *7; A atbox = My_min (box_2,box_3,box_5,box_7);//every time you insert the smallest one into an array, you don't have to sort the - if(box = = box_2)//always use if if, because the results may be the same -I_2 + +; - if(Box = =box_3) -I_3 + +; - if(Box = =box_5) inI_5 + +; - if(Box = =box_7) toI_7 + +; + -i + +; theS[i] =box; * } $ Panax Notoginseng while(SCANF ("%d", &n) &&N) - { thebox = n -; + if(Box = = One|| box = = A|| box = = -) A { thestrcpy (CHA,"th"); + Gotolabel; - } $ $box =N; - while(Box/Ten) -Box%=Ten; the Switch(Box%Ten) - {Wuyi Case 1: strcpy (Cha,"St"); Break; the Case 2: strcpy (Cha,"nd"); Break; - Case 3: strcpy (Cha,"Rd"); Break; Wu default: strcpy (Cha,"th"); Break; - } About $ Label: -printf"The %d%s humble number is%d.\n", N,cha,s[n-1]); - } - A return 0; + } the - intMy_min (intAintBintCintd) $ { the intMin =A; the themin = min < b?min:b; themin = min < c?Min:c; -min = min < d?min:d; in the returnmin; the}
HDU 1058 Humble Numbers (DP)