C/C++教程

2021-10-21-走台阶的C++问题,壮壮的程序和我的程序

本文主要是介绍2021-10-21-走台阶的C++问题,壮壮的程序和我的程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

==以下是壮壮的程序=
#include <bits/stdc++.h>
using namespace std;
int v[1005];
int memo[1005];

int Max(int n){
if(memo[n] != 0){
return memo[n];
}
if(n == 0) //ÉèÖóõ״̬
return 0;
if(n == 1)
return v[0];
int max = Max(n-2); //ÕÒ¸ü´óµÄÖµ
if(max < Max(n-1))
max = Max(n-1);
memo[n] = v[n-1]+max;
return v[n-1]+max;
}

int main(){
int n; //ÊäÈë
cin >> n;
for(int i = 0;i < n;i++){
cin >> v[i];
} //Êä³ö
cout << Max(n);
return 0;
}
=壮壮程序结束==
==下面是我自己的程序
//this version uses pointer to get input array
//Ö±½Ó¶ÁÈënºÍÊý×飬²»»ØÏÔ ,×îºó»ØÏÔ½á¹û
#include
using namespace std;

int n=0;
int *a;
int *sign;

int h(int m){
if(m<0)
{
cout<<endl<<“error occured ,m <0, m=”<<m;//²»ÄܳöÏÖmСÓÚ0
return -1;
}

if(m==0){ 	//´ËʱΪµÚ0¸öÔªËØ£¬ 
	sign[m]=1;
	return 0;//now the highest sum is 0
}

//Ö»Òªm´óÓÚ0£¬¾ÍÏȼÆËãÇ°ÃæµÄºÍ¡£ 
h(m-1);

if(m==n){	//now m is the last ,ie highest step , should be added in
	sign[m]=1;
	return (h(m-1)+a[m]);
}

if(a[m]>=0){ 	//none negative ,must be added in
	sign[m]=1;		//stepping on a[m]
	return (h(m-1)+a[m]);
}


 
// below is a[m]<0,2 cases ,
//1,jumptable;
//2,non jumptable;

//1. jumptable
if (sign[m-1]>0){	//jumptable;
	sign[m]=0;// jump over  a[m]
			
	return h(m-1);
}
//below is a[m-1] is also <0, shuld make a comparison between a[m-1] and a[m];

if ( sign[m-1]==0 ) {
	// 2 cases :
	// case 1: a[m-1]<=a[m]
	// case 2: a[m-1]>a[m]

	if(a[m-1]<=a[m]) {
		sign[m]=1;
		return (h(m-1)+a[m]);
	}
 	//below is (a[m-1]>a[m]
	// step on a[m-1],jump over a[m]
	sign[m-1]=1;
	sign[m]=0;
	return (h(m-1)+a[m-1]);
}
//below is a[m-1] stepped ,then jump over a[m]
sign[m]=0;
return h(m-1);

}

int main(){

//cout<<endl<<"input a n:";
cin>>n;
//cout<<"the n you input is :"<<n;

int array_input[n+1]; 	//ÊäÈëµÄÊý×Ö£¬´Óa[1]¿ªÊ¼´æ·Å£¬¶ø·Ça[0] 
int array_sign[n+1];
 
for(int i=1;i<=n;++i)	{
	//cout<<endl<<"input array_input["<<i<<"]=";
	array_sign[i]=-1;
	cin>>array_input[i];

	//cout<<endl<<"arrary_input["<<i<<"]="<<array_input[i];
	//cout<<endl<<"arrary_sign["<<i<<"]="<<array_sign[i];

}


a=array_input;
sign=array_sign;


cout<<h(5);	

}

==========我自己的程序结束

这篇关于2021-10-21-走台阶的C++问题,壮壮的程序和我的程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!