Let's consider a triangle of numbers in which one number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a logic which will compute the largest of the sums of numbers that appear on the paths starting from the top towards the base, so that:1. On each path the next number is located on the row below, more precisely either directly below or below and one place to the right;2. The number of rows is strictly positive, but less than 1003. All numbers are positive integers between 0 and 99.
#include <stdio.h>
int main()
{
int i,n,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int ar[n][n];
for(i=0;i<n;i++){
for(j=0;j<=i;j++){
scanf("%d",&ar[i][j]);
scanf("\n");}
}
for(i=n-1;i>=1;i--)
{
for(j=0;j<i;j++)
{if(ar[i][j]>ar[i][j+1])
ar[i-1][j]+=ar[i][j];
else ar[i-1][j]+=ar[i][j+1];
}
}
printf("%d\n",ar[0][0]);
}
return 0;
}
Comments
Post a Comment