【HDOJ】1329 Hanoi Tower Troubles Again!

水题,搞清楚hanoi的定义就好做了。

 1 /* 1329 */
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 
 7 #define MAXN 55
 8 
 9 int b[MAXN];
10 int a[MAXN];
11 
12 bool isSquare(int x) {
13     int y = (int) sqrt(x*1.0);
14     
15     return y*y == x;
16 }
17 
18 void init() {
19     int i, j, k;
20     int n = 1;
21     bool flag;
22     
23     memset(b, 0, sizeof(b));
24     for (i=1; ; ++i) {
25         flag = true;
26         for (j=0; j<n; ++j) {
27             if (b[j]==0 || isSquare(b[j]+i)) {
28                 flag = false;
29                 b[j] = i;
30                 break;
31             }
32         }
33         if (flag) {
34             a[n] = i-1;
35             b[n++] = i;
36             if (n > 50)
37                 break;
38         }
39     }
40 }
41 
42 int main() {
43     int t, n;
44     
45     #ifndef ONLINE_JUDGE
46         freopen("data.in", "r", stdin);
47     #endif
48     
49     init();
50     scanf("%d", &t);
51     while (t--) {
52         scanf("%d", &n);
53         printf("%d\n", a[n]);
54     }
55     
56     return 0;
57 }

 

posted on 2015-01-15 20:52  Bombe  阅读(239)  评论(0编辑  收藏  举报

导航