The following program is my ACcepted code for PKU OJ 1163 The Triangle
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃PKU 1163的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
點這裡看題目 Click here to see this Problem!
//This program is for PKU OJ 1163 The Triangle
//http://poj.org/problem?id=1163
/*
I calculate the max sum from the button of the triangle,
compare each pair of the row
(ex: in row 5, there are 5 pairs of 2 number. (4, 5), (5, 2), (2, 6), (6, 5) )
choose the larger one because that can cause higher sum to the upper number.
continuously add higher number to the upper row until to the row 1!
The the number of row 1 is the highest sum.
由三角形的最下面開始計算最大合
先將每個row分開成2個數字一對 (例如在row 5會有五對 (4, 5), (5, 2), (2, 6), (6, 5))
每一個配隊中選擇將大的那個數字往上加
這樣即可得到目前最下面row的較大值
持續比較翰把較大的數字網上面的row加上去
直到加到row 1時就會得到最大的合
*/
#include<stdio.h>
int main()
{
int row, n[101][101];
while(scanf("%d", &row)!=EOF)
{
for(int i=1; i<= row; i++)
for(int j=1; j<=i; j++)
scanf("%d", &n[i][j]);
for(int i=row ; i>=2 ;i--)
for(int j=1; j<=i; j++)
{
if(n[i][j] > n[i][j+1])
n[i-1][j] += n[i][j];
else
n[i-1][j] += n[i][j+1];
}
printf("%d\n", n[1][1]);
}
return 0;
}
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃PKU 1163的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
點這裡看題目 Click here to see this Problem!
//This program is for PKU OJ 1163 The Triangle
//http://poj.org/problem?id=1163
/*
I calculate the max sum from the button of the triangle,
compare each pair of the row
(ex: in row 5, there are 5 pairs of 2 number. (4, 5), (5, 2), (2, 6), (6, 5) )
choose the larger one because that can cause higher sum to the upper number.
continuously add higher number to the upper row until to the row 1!
The the number of row 1 is the highest sum.
由三角形的最下面開始計算最大合
先將每個row分開成2個數字一對 (例如在row 5會有五對 (4, 5), (5, 2), (2, 6), (6, 5))
每一個配隊中選擇將大的那個數字往上加
這樣即可得到目前最下面row的較大值
持續比較翰把較大的數字網上面的row加上去
直到加到row 1時就會得到最大的合
*/
#include<stdio.h>
int main()
{
int row, n[101][101];
while(scanf("%d", &row)!=EOF)
{
for(int i=1; i<= row; i++)
for(int j=1; j<=i; j++)
scanf("%d", &n[i][j]);
for(int i=row ; i>=2 ;i--)
for(int j=1; j<=i; j++)
{
if(n[i][j] > n[i][j+1])
n[i-1][j] += n[i][j];
else
n[i-1][j] += n[i][j+1];
}
printf("%d\n", n[1][1]);
}
return 0;
}
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!