爬楼梯求最大分数
如下图,最大分数是: 10+20+25+20=75.
要求:
1、每次只能走一步或者两步;
2、不能连续三步走一样的,即最多连续走两次一步,或者连续走两次两步;
3、必须走到最后一层台阶。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; #define MAX 301
#define max(a, b) ((a) > (b) ? (a) : (b)) int Answer, N;
int stairs[MAX];
int dp[MAX]; int main( int argc, char** argv )
{
int T, test_case; freopen( "input_climingstairs.txt", "r", stdin ); cin >> T;
for( test_case = ; test_case < T; test_case++ )
{
Answer = ; cin >> N; memset( dp, , sizeof( dp ) ); for( int i = ; i <= N; i++ )
{
cin >> stairs[i];
} dp[] = ;
dp[] = stairs[];
dp[] = stairs[] + dp[];
for( int j = ; j <= N; j++ )
{
dp[j] = max( dp[j-], dp[j-] + stairs[j-] ) + stairs[j];
} Answer = dp[N]; cout << Answer << endl;
} return ;
}
输入文件: