// http://poj.org/problem?id=1163
#include <iostream>
#include <stdlib.h> /*为了使用malloc*/
int No_1163(){
int rows = 0;
scanf("%d",&rows); //输入三角形行数 rows
int totalNumbs = (1+rows )*rows/2; //计算三角形所包含的所有位置数
int *a = (int*) malloc (totalNumbs*sizeof(int));
//输入过程:从标准输入读入所有位置上的数值
int count = 0;
for (int i = 1;i<=rows;i++){//i 表示第i行
for (int j= 1;j <= i;j++){ // j,每行读入j个数字;
scanf("%d",&(a[count]));
count++;
}
}
//数据处理过程:从倒数第二行开始,计算每个位置与他下一行相邻两个位置数字和的最大值
for (int i = rows-1; i>=1 ;i--){//i 表示第i行 ,从第1行到第n行
int startIndex = i*(i-1)/2;
int nextStartIndex = (i+1)*i/2;
for(int j = 0;j<i;j++){
a[startIndex+j] +=
a[nextStartIndex+j]>a[nextStartIndex+j+1] ? a[nextStartIndex+j]:a[nextStartIndex+j+1];
}
}
//输出过程:
printf("%d",a[0]);
free(a);
return 0;
}
int main(int argc, char** argv) {
No_1163();
return 0;
}
[ACM] #POJ#1163
最新推荐文章于 2025-09-10 14:02:06 发布
