码迷,mamicode.com
首页 > 其他好文 > 详细

SGU - 123 - The sum (简单数学!)

时间:2014-12-11 17:23:40      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:acm   algorithm   c++   uva   iostream   

SGU - 123
Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u

 Status

Description

Here is your second problem, keep calm and solve it .

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

 

Input

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

 

Output

First line should contain number S.

 

Sample Input

5

 

Sample Output

12

Source




简单数学求和题,,居然int都不会爆,,刚开始我还以为要long long。。


AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;

int a[42] = {0,1,1};

int main()
{
	int k;
	for(int i=3; i<41; i++)
	{
		a[i] = a[i-1] + a[i-2];
	}	
	scanf("%d", &k);
	int ans = 0;
	for(int i=1; i<=k; i++)
	{
		ans += a[i];
	}
	printf("%d\n", ans);
	return 0;
} 


SGU - 123 - The sum (简单数学!)

标签:acm   algorithm   c++   uva   iostream   

原文地址:http://blog.csdn.net/u014355480/article/details/41869115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!