INPUT & OUTPUT FORMAT:
Input consists of 2 integers and 1 2D-array. Integers correspond to the size of rows and columns.
SAMPLE INPUT:
3
3
1 2 3
4 5 6
7 8 9
SAMPLE OUTPUT:
Sum of Zig-Zag pattern is 35
SAMPLE INPUT:
2
2
1 2
4 5
SAMPLE OUTPUT:
Sum of Zig-Zag pattern is 12
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int r, c, i, j, sum = 0;
cin>>r>>c;
int mat[r][c];
for(i = 0; i < r; i++){
for(j = 0; j < c; j++){
cin>>mat[i][j];
}
}
for(j = 0; j < c; j++){
sum += mat[0][j];
}
for(i = 1; i <= r-2; i++){
for(j = c-2; j > 0; j--){
sum += mat[i][j];
}
}
for(j = 0; j < c; j++){
sum += mat[r-1][j];
}
cout<<"Sum of Zig-Zag pattern is "<<sum;
return 0;
}
No comments:
Post a Comment