Input Format:
The input consists of (n*n+1) integers.
The first integer corresponds to the number of rows/columns in the matrix.
The remaining integers correspond to the elements in the matrix.
The elements are read in row-wise order, the first row first, then second row and so on.
Assume that the maximum value of m and n is 5.
Output Format:
Print yes if it is a magic square. Print no if it is not a magic square.
Sample Input:
2
4 5
5 4
Sample Output:
No
SOLUTION:
#include<iostream>
using namespace std;
int main(){
int i, j, n;
cin>>n;
int Arr[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cin>>Arr[i][j];
}
}
int D1 = 0;
int D2 = 0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j){
D1=D1+Arr[i][j];
}
if((i+j)==n-1){
D2=D2+Arr[i][j];
}
}
}
if(D1 == D2){
int row_sum = 0, col_sum = 0;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
row_sum += Arr[i][j];
col_sum += Arr[j][i];
}
}
if(row_sum == col_sum){
cout<<"Yes";
}
else{
cout<<"No";
}
}
else{
cout<<"No";}
}
No comments:
Post a Comment