SGU 123: The sum

Jasonlin posted @ 2011年3月31日 13:43 in SGU with tags 斐波那契数 , 2181 阅读

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=123

题目大意:

      求斐波那契数前n个的和。

解题思路:

      一个数组存斐波那契数,一个数组存前n个的和。

解题代码:

     

#include<iostream>
using namespace std;
long long f[42]={1,1};
long long s[42]={1,2};
int main(){
	for(int i=2;i<42;i++){
		f[i]=f[i-1]+f[i-2];
		s[i]=s[i-1]+f[i];
	}
	int n;
	while(cin>>n)
		cout<<s[n-1]<<endl;
	return 0;
}

扩展知识:http://en.wikipedia.org/wiki/Fibonacci_number

  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter