3 条题解
-
019220448 董文杰 (董文杰) LV 9 @ 2022-10-08 15:59:23
//C语言 递归法 #include<stdio.h> int fab(int); int main() { int k1, k2; scanf("%d%d", &k1,& k2); int sum = 0; for (int i = k1; i <= k2; i++) { sum = sum + fab(i); } printf("%d", sum); return 0; } int fab(int n) { if (n == 1 || n == 2) return 1; else return fab(n - 1) + fab(n - 2); }
-
02021-07-13 10:55:45@
#include<bits/stdc++.h> using namespace std; int a[10005]; int main() { int x,y,sum=0; cin>>x>>y; for(int i=1;i<=y;i++) cin>>a[i]; a[1]=1; a[2]=1; for(int i=3;i<=y;i++) a[i]=a[i-2]+a[i-1]; for(int i=x;i<=y;i++) sum+=a[i]; cout<<sum<<endl; return 0; }
-
-32019-04-09 16:14:11@
#include<stdio.h> int Fibonacci(int K); int main() { int k1, k2; int s1, s2, s3; int sum = 0; int num; scanf("%d", &k1); scanf("%d", &k2); if (k1 > k2) { return -1; } if (k1 == 1) { s1 = 1; s2 = 0; } else if (k1 == 2) { s1 = 0; s2 = 1; } else { s1 = Fibonacci(k1 - 2); s2 = Fibonacci(k1 - 1); } for (int i = k1; i <= k2; i++) { num = s1 + s2; sum = sum + num; s3 = s2; s2 = s1 + s2; s1 = s3; } printf("%d", sum); return 0; } //非递归做法 int Fibonacci(int K) { int a1 = 1; int a2 = 1; int a3, an; if (K <= 0) { return -1; } if (K == 1 || K == 2) { return 1; } for (int i = 0; i < K-2; i++) { an = a1 + a2; a3 = a2; a2 = a1 + a2; a1 = a3; } return an; } /* // 递归做法 int Fibonacci(int K) { if (K == 2 || K == 1) { return 1; } else { return Fibonacci(K - 1)+Fibonacci(K - 2); } } */
- 1
信息
- 难度
- 6
- 分类
- (无)
- 标签
- 递交数
- 2022
- 已通过
- 564
- 通过率
- 28%
- 被复制
- 6
- 上传者