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

 

转载于:https://www.cnblogs.com/bianjunting/p/10503979.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值