滚动数组

例如斐波那契数列用滚动数组优化

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <memory>
 7 #include <iostream>
 8 #define LL long long
 9 using namespace std;
10 
11 int fei[3];
12 
13 int fun(int n){
14     fei[1]=0;
15     fei[2]=1;
16     for(int i=2;i<=n;i++){
17         fei[0]=fei[1];
18         fei[1]=fei[2];
19         fei[2]=fei[0]+fei[1];
20     }
21     return fei[2];
22 }
23 
24 int main(){
25     int n;
26     scanf("%d",&n);
27     int ans=fun(n);
28     printf("%d\n",ans);
29 }
View Code

 

posted @ 2016-03-22 22:56  yyblues  阅读(408)  评论(0编辑  收藏  举报