对于先输入大n再输入小n,小n答案可以直接输出
#include<stdio.h>
int main()
{
int n, temp = 2;
int a[20];
a[0] = 1; a[1] = 2;
while(scanf("%d", &n) != EOF){
if(n <= temp) printf("%d\n", a[n - 1]);
else{
for(int i = temp;i < n;i++)
a[i] = a[i - 1] + a[i - 2];
printf("%d\n", a[n - 1]);
temp = n;
}
}
}
登录后发布评论
暂无评论,来抢沙发