【HDOJ】1058 Humble Numbers

简单题,注意打表,以及输出格式。这里使用了可变参数。

 1 #include <stdio.h>
 2 
 3 #define MAXNUM 5845
 4 #define ANS    2000000000
 5 
 6 int buf[MAXNUM];
 7 int min(int a, int b) {
 8     return a<b ? a:b;
 9 }
10 
11 int minn(int argc, ...) {
12     int i, tmp;
13     int *arg = &argc + 1;
14 
15     tmp = *arg;
16     for (i=1; i<argc; ++i)
17         tmp = min(tmp, *++arg);
18 
19     return tmp;
20 }
21 
22 int main() {
23     int i;
24     int a, b, c, d;
25     int ia, ib, ic, id;
26     ia = ib = ic = id = 1;
27 
28     i = 1;
29     buf[1] = 1;
30 
31     a = 2 * buf[ia];
32     b = 3 * buf[ib];
33     c = 5 * buf[ic];
34     d = 7 * buf[id];
35     while (buf[i] < ANS) {
36         buf[++i] = minn(4, a,b,c,d);
37         if (buf[i] == a)  a = 2*buf[++ia];
38         if (buf[i] == b)  b = 3*buf[++ib];
39         if (buf[i] == c)  c = 5*buf[++ic];
40         if (buf[i] == d)  d = 7*buf[++id];
41     }
42 
43     while (scanf("%d", &i)!=EOF && i) {
44         if (i%10==1 && i%100!=11)
45             printf("The %dst humble number is %d.\n", i, buf[i]);
46         else if (i%10==2 && i%100!=12)
47             printf("The %dnd humble number is %d.\n", i, buf[i]);
48         else if (i%10==3 && i%100!=13)
49             printf("The %drd humble number is %d.\n", i, buf[i]);
50         else
51             printf("The %dth humble number is %d.\n", i, buf[i]);
52     }
53 
54     return 0;
55 }

 

posted on 2014-03-31 18:16  Bombe  阅读(137)  评论(0编辑  收藏  举报

导航