1 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
30
//c++ #include<iostream> #define max(a,b) ((a)>(b)?(a):(b)) using namespace std; int main() { int n,i,j,c,a[10100]; cin>>c; while(c--) { cin>>n; int s=(n+1)*n/2; for(i=1;i<=s;++i) { cin>>a[i]; } for(i=s-n,j=0;i>=1;--i) { a[i]=a[i]+max(a[i+n-1],a[i+n]); if(++j==n-1) { j=0; --n; } } cout<<a[1]<<endl; } return 0; }
//C语言 #include <stdio.h> #define N 10000 #define Max(a,b) ((a) > (b) ? (a) : (b)) int a[N]; int main() { int n,m,i,k,j; scanf("%d",&m); while(m-- > 0) { scanf("%d",&n); k = (1 + n) * n / 2; for(i = 1 ; i <= k; i++) { scanf("%d",a+i); } k = k - n; for(i = k , j = 0 ; i >= 1 ; i--) //从下到上,从右到左加起来 { a[i] = a[i] + Max(a[i+n],a[i+n-1]); // 因为每一个都是 相邻的,所以可以这样 if(++j == n-1) { n--; j = 0; } } printf("%d\n",a[1]); } return 0; }