解题分析:
既然让求因子只能为2357的数, 那么分别看2357的倍数就OK了。
另外。输出时要注意st(first), nd(secend), rd(third)
因为11,12,13时为th 所以111和101也是不同的
代码如下:
#include <stdio.h> #define n 5900 int hum[n]; int min_num(int a, int b, int c, int d){ int t = a; if(t>b) t = b; if(t>c) t = c; if(t>d) t = d; return t; } int main(){ int a1, a2, a3, a4, b1, b2, b3, b4, m; int i; hum[1] = 1; a1 = a2 = a3 = a4 = 1; for(i=2; i<=n; i++){ b1 = hum[a1] * 2; b2 = hum[a2] * 3; b3 = hum[a3] * 5; b4 = hum[a4] * 7; hum[i] = min_num(b1, b2, b3, b4); if(b1 == hum[i]) a1++; if(b2 == hum[i]) a2++; if(b3 == hum[i]) a3++; if(b4 == hum[i]) a4++; } while(scanf("%d", &m) == 1 && m != 0){ if(m % 10 == 1 && m % 100 != 11) printf("The %dst humble number is %d.\n", m, hum[m]); else if(m % 10 == 2 && m % 100 != 12) printf("The %dnd humble number is %d.\n", m, hum[m]); else if(m % 10 == 3 && m % 100 != 13) printf("The %drd humble number is %d.\n", m, hum[m]); else printf("The %dth humble number is %d.\n", m, hum[m]); } return 0; }