文章
11
粉丝
414
获赞
9
访问
108.3k
//骨牌铺方格 (找规律)
#include<iostream>
using namespace std;
int sum(int n);
long long rem[1024] = {0};
int main(){
int n,scheme;
cin >> n;
while(n){
scheme = sum(n);
cout << scheme << endl;
cin >> n;
}
return 0;
}
int sum(int n){
int res = 1;
if(rem[n])
res = rem[n]; //rem[]数组用于优化递归,记忆中间结果减少重复运算
else if(n == 1 || n ==2)
res = n;
else if(n==3)
res = n + 1;
else
res = sum(n - 1) + sum(n - 2) + sum(n - 3);
return res;
}
登录后发布评论
暂无评论,来抢沙发