南阳18--The Triangle
生活随笔
收集整理的這篇文章主要介紹了
南阳18--The Triangle
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
The Triangle
時間限制:1000?ms ?|? 內(nèi)存限制:65535?KB 難度:4 描述7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
?
?
1 #include<stdio.h> 2 int main() 3 { 4 int n,i,j,num[110][110]; 5 scanf("%d",&n); 6 for(i=0;i<n;i++) 7 for(j=0;j<=i;j++) 8 scanf("%d",&num[i][j]); 9 for(i=n-2;i>=0;i--) 10 for(j=0;j<=i;j++) 11 { 12 num[i][j]+=num[i+1][j]>num[i+1][j+1]?num[i+1][j]:num[i+1][j+1]; 13 } 14 printf("%d\n",num[0][0]); 15 return 0; 16 }?
重刷:
#include <cstdio> #include <cstring> #define max(a, b) (a>b?a:b) const int N = 101; int dp[N][N], num[N][N]; int main(){int n;while(scanf("%d", &n) != EOF){memset(dp, 0, sizeof(dp));for(int i = 1; i <= n; i++)for(int j = 1; j <= i; j++)scanf("%d", &num[i][j]);for(int i = 1; i <= n; i++)for(int j = 1; j <= i; j++)dp[i][j]=max(dp[i-1][j], dp[i-1][j-1])+num[i][j];int max = -1;for(int i = 1; i <= n; i++)if(dp[n][i] > max)max = dp[n][i];printf("%d\n", max); }return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/soTired/p/4579513.html
總結(jié)
以上是生活随笔為你收集整理的南阳18--The Triangle的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QT自定义控件(生成和使用)
- 下一篇: FTP 500 OOPS