首页 > 代码库 > SGU - 123 - The sum (简单数学!)
SGU - 123 - The sum (简单数学!)
SGU - 123 The sum
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 (简单数学!)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。