数字三角形

//数字三角形 
/*
#include<stdio.h>
#define MAX_NUM 100
int D[MAX_NUM+10][MAX_NUM+10];
int N;
int MaxSum(int r,int j)
{
	if(r==N)
		return D[r][j];
	int nSum1=MaxSum(r+1,j);
	int nSum2=MaxSum(r+1,j+1);
	if(nSum1>nSum2)
		return nSum1+D[r][j];
	return nSum2+D[r][j];
}
int main()
{
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
	 for(int j=1;j<=i;j++)
	 	scanf("%d",&D[i][j]);
	printf("%d\n",MaxSum(1,1));
	return 0;
}
*/
/*
#include<stdio.h>
#include<memory.h>
#define MAX_NUM 100
int D[MAX_NUM+10][MAX_NUM+10];
int N;
int aMaxSum[MAX_NUM+10][MAX_NUM+10];
int MaxSum(int r,int j)
{
	if(r==N)
		return D[r][j];
	if(aMaxSum[r+1][j]==-1)
		aMaxSum[r+1][j]=MaxSum(r+1,j);
	if(aMaxSum[r+1][j+1]==-1)
		aMaxSum[r+1][j+1]=MaxSum(r+1,j+1);
	if(aMaxSum[r+1][j]>aMaxSum[r+1][j+1])
		return aMaxSum[r+1][j]+D[r][j];
	return aMaxSum[r+1][j+1]+D[r][j];
}
int main()
{
	int m;
	scanf("%d",&N);
	memset(aMaxSum,-1,sizeof(aMaxSum));
	for(int i=1;i<=N;i++)
		for(int j=1;j<=i;j++)
			scanf("%d",&D[i][j]);
	printf("%d\n",MaxSum(1,1));
	return 0;
}
*/
#include<stdio.h>
#include<memory.h>
#define MAX_NUM 100
int D[MAX_NUM+10][MAX_NUM+10];
int N;
int aMaxSum[MAX_NUM+10][MAX_NUM+10];
int main()
{
	int i,j;
	scanf("%d",&N);
	for(i=1;i<=N;i++)
		for(j=1;j<=i;j++)
			scanf("%d",&D[i][j]);
	for(j=1;j<=N;j++)
		aMaxSum[N][j]=D[N][j];
	for(i=N;i>1;i--)
		for(j=1;j<i;j++)
		{
			if(aMaxSum[i][j]>aMaxSum[i][j+1])
				aMaxSum[i-1][j]=aMaxSum[i][j]+D[i-1][j];
			else 
				aMaxSum[i-1][j]=aMaxSum[i][j+1]+D[i-1][j];
		}
	printf("%d",aMaxSum[1][1]);
	return 0;
}

你可能感兴趣的:(c,poj)