题解 | 牛牛学数列6
牛牛学数列6
https://www.nowcoder.com/practice/b6321648517247b2ac2e2f80cbc63ae1
// #include <stdio.h>
// int main() {
// int n;
// scanf("%d", &n);
// int shuzu[n];
// if (n >= 1) shuzu[0] = 0;
// if (n >= 2) shuzu[1] = 1;
// if (n >= 3) shuzu[2] = 1;
// if (n == 1){
// printf("%d", shuzu[0]);
// }
// if ((n == 2) || (n == 3)){
// printf("%d", shuzu[1]);
// }
// if (n >= 4){
// for (int i = 3; i < n; i++){
// shuzu[i] = shuzu[i - 3] + 2 * shuzu[i - 2] + shuzu[i - 1];
// }
// }
// printf("%d", shuzu[n - 1]);
// return 0;
// }
//更简便的写法
#include <stdio.h>
int main(){
int n;
scanf("%d", &n);
int a = 0, b = 1, c = 1, d;
if (n == 1){
printf("%d", a);
return 0;
}
if (n == 2 || n == 3){
printf("%d", b);
return 0;
}
for(int i = 4; i <= n; i++){
d = c + 2 * b + a;
a = b;
b = c;
c = d;
}
printf("%d", c);
return 0;
}
OPPO公司福利 1229人发布
