zoukankan      html  css  js  c++  java
  • 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
    "); 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("
    ");
    36     }
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    杭电 1013 Digital Roots
    杭电 1040 As Easy As A+B 【排序】
    杭电 2092 整数解
    bzoj3223
    bzoj3224
    LA3905
    bzoj3601
    bzoj1002
    bzoj3105
    bzoj3332
  • 原文地址:https://www.cnblogs.com/bianjunting/p/10503979.html
Copyright © 2011-2022 走看看