Description: The algorithm detailed below will have you calculating different ty
ID: 3767224 • Letter: D
Question
Description: The algorithm detailed below will have you calculating different types of summations, and then comparing each answer to find the greatest summation. Here are the four summations to compute
Left to Right: For this calculation, we want both the top left to right calculation, and the bottom, so it will look like so
Top to Bottom: For this calculation, we want both the top to bottom at the beginning and the end
Diagonal: For this calculation, we want both the left to right diagonal, and right to left.
Diagonal SumThe answer here would be 34 + 22 = 56
Skip: This algorithm will take the sum of every odd index on odd lines, and even indexes on even lines
Once all have been computed, they need to be compared to each other. So 56 > 52 > 48 > 35. Therefore in this situation, 56 would be greatest, and we would print out 56
Input: n = the size of the n * n square matrix and the number of inputs per line
A(i) = will be the ith number of the [n]th position of the array
Sample input:
4
9 13 5 2
1 11 7 6
3 7 4 1
6 0 7 10
sample output:
56
Input Format
n n is the size of the n*n matrix a[n][n] n numbers of each line, n lines
Output Format
x x is just the greatest sum
Sample Input
4
9 13 5 2
1 11 7 6
3 7 4 1
6 0 7 10
Sample Output
56
Explanation
I don't know what this is
TEMPLATE:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Explanation / Answer
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n ;
cin>>n;
int A[n][n];
int sum1=0,sum2=0,sum3=0,sum4=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>A[i][j];
if((i+j)%2 == 0)
sum4 += A[i][j];
}
}
for(int j=0;j<n;j++){
sum1 += A[0][j] + A[n-1][j];
sum2 += A[j][0] + A[j][n-1];
sum3 += A[j][j] + A[j][n-1-j];
}
int max1 = (sum1 > sum2) ? sum1 : sum2;
int max2 = (sum3 > sum4) ? sum3 : sum4;
int max = (max1 > max2) ? max1 : max2;
cout<<max;
return 0;
}