題目描述
如果一個數的素因子只包含2,3,5或7,那么我們把這種數叫做丑數。序列1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27...展示了前20個丑數。
請你編程尋找這個序列中的第n個元素。
請你編程尋找這個序列中的第n個元素。
輸入
輸入包含多組測試數據。每組輸入為一個整數n(1<=n<=5842),當n=0時,輸入結束。
輸出
對於每組輸入,輸出一行“The nth humble number is number.”。里面的n由輸入中的n值替換,“st”,“nd”,“rd”和“th”這些序數結尾的用法參照輸出樣例。
樣例輸入
1 2 3 4 11 12 13 21 22 23 100 1000 5842 0
樣例輸出
The 1st humble number is 1. The 2nd humble number is 2. The 3rd humble number is 3. The 4th humble number is 4. The 11th humble number is 12. The 12th humble number is 14. The 13th humble number is 15. The 21st humble number is 28. The 22nd humble number is 30. The 23rd humble number is 32. The 100th humble number is 450. The 1000th humble number is 385875. The 5842nd humble number is 2000000000.
第一次讀題的時候,以為只要可以被2,3,5除盡的就是丑數,所以先后用5,3,2對待判斷數取余,最后判斷是否余0就行了,但是丑數是不能有除2,3,5,之外的素數因子,是允許有合數因子的。
正確做法:從1開始,丑數的2,3,5倍也都是丑數,每求出一個丑數的倍數,用set來去重,如果set中沒有,扔優先隊列和set中。每次從優先隊列中取出的數就是這次的丑數,判斷是否是第n個數。
是的話輸出,不是的話求出它的2,3,5倍,重復下去。。。。。。。。
記得要用 long long