HDU-1715.(大菲波数)

  本题大意:给定一个n,让你计算并输出第n个菲波数。

  本题思路:主要就是模拟加法,其它都好说。

  

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 const int maxn = 1e5;
 7 int num, n, now, idx, tot1, tot2;
 8 struct Fbn {
 9     int front[maxn], ans[maxn];
10 } fbn;
11 
12 int main () {
13     scanf("%d", &num);
14     while(num --) {
15         scanf("%d", &n);
16         memset(fbn.front, 0, sizeof(fbn.front));
17         memset(fbn.ans, 0, sizeof(fbn.ans));
18         fbn.ans[0] = fbn.front[0] = 1;
19         if(n == 1 || n == 2) { printf("1\n"); continue;}
20         tot1 = tot2 = 1;
21         for(now = 3; now <= n; now ++) {
22             int i = 0, j = 0, c = 0;
23             while(i < tot1 && j < tot2) {
24                 int t = fbn.ans[i];
25                 fbn.ans[i] = (fbn.ans[i] + fbn.front[j] + c) % 10;
26                 c = (t + fbn.front[j] + c) / 10;
27                 fbn.front[j] = t;
28                 i ++, j ++;
29                 if(c && i == tot1) tot1 ++;
30                 if(tot1 > tot2) tot2 ++;
31             }
32         }
33         for(int i = tot1 - 1; i >= 0; i --)
34             printf("%d", fbn.ans[i]);
35         printf("\n");
36     }
37     return 0;
38 }
View Code

 

posted @ 2019-03-10 01:38  Cruel_King  阅读(237)  评论(0编辑  收藏  举报