zoukankan      html  css  js  c++  java
  • SGU123 The sum

    123. The sum

    time limit per test: 0.5 sec. 
    memory limit per test: 4096 KB

     

    The Fibonacci sequence of numbers is known: F1 = 1; F2 = 1; Fn+1 = Fn + Fn-1, for n>1. You have to find S - the sum of the first K Fibonacci numbers.

     

    Input

    First line contains natural number K (0<K<41).

     

    Output

    First line should contain number S.

     

    Sample Input

    5
    

     

    Sample Output

    12
    

    Author : Paul "Stingray" Komkoff, Victor G. Samoilov
    Resource : 5th Southern Subregional Contest. Saratov 2002
    Date : 2002-10-10

    题解:迭代一下就行。。。数组都不用开。。。

    View Code
     1 #include<stdio.h>
     2 int main(void)
     3 {
     4     long f1,f2,f3,k,i;
     5     long long ans;
     6     scanf("%ld",&k);
     7     if(k==1)
     8     {
     9         printf("1\n");
    10         return 0;
    11     }
    12     ans=2;
    13     i=3;
    14     f1=1;
    15     f2=1;
    16     while(i<=k)
    17     {
    18         f3=f1+f2;
    19         ans+=f3;
    20         f1=f2;
    21         f2=f3;
    22         i++;
    23 
    24     }
    25     printf("%lld\n",ans);
    26     return 0;
    27 }
  • 相关阅读:
    python
    C++的socket编程学习
    GooglePlay
    GooglePlay
    Admob
    cocos2dx
    cocos2dx
    cocos2dx
    cocos2dx
    浅谈白鹭Egret
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2955012.html
Copyright © 2011-2022 走看看