看的題解。。。不知道這些方法都是怎麼想出來的,好厲害啊,估計我是想不出來的,用b2,b3,b5,b7來分別記錄
從第幾位數開始還沒有乘對應的2,3,5,7然後比較,小的數排上就ok了。。。注意有的時候是、會出現乘積相同,
所以都得加一,比如2*3和3*2,都得讓b2,b3加一。這道題還涉及到了英語上的知識,就是只有11,12,13是序數
詞時加th,1,2,3,分別是first,second,third。
代碼:
#include#include #include #include #include #include #include #include #include using namespace std; int main() { int a[5850]; int temp,n; a[1] = 1; int b2,b3,b5,b7; b2 = b3 = b5 = b7 = 1; int m = 2; while(m<5843) { temp = min(2*a[b2],min(3*a[b3],min(5*a[b5],7*a[b7]))); a[m++] = temp; if(temp == 2*a[b2]) b2++; if(temp == 3*a[b3]) b3++; if(temp == 5*a[b5]) b5++; if(temp == 7*a[b7]) b7++; } while(cin >> n,n) { int x = a[n]; //printf(The 1st humble number is 1.) if(n%100==11 || n%100 == 12 || n%100 == 13) printf(The %dth humble number is %d. ,n,x); else if(n%10 == 1) printf(The %dst humble number is %d. ,n,x); else if(n%10 == 2) printf(The %dnd humble number is %d. ,n,x); else if(n%10 == 3) printf(The %drd humble number is %d. ,n,x); else printf(The %dth humble number is %d. ,n,x); } return 0; }