-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormingAMagicSquare.cpp
55 lines (39 loc) · 1.24 KB
/
FormingAMagicSquare.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// problem: "https://www.hackerrank.com/challenges/magic-square-forming/problem"
#include <bits/stdc++.h>
#include<math.h>
using namespace std;
int main()
{
int G[3][3],i,j,sum[8],min;
int A[3][3]={8,1,6,3,5,7,4,9,2};
int B[3][3]={4,3,8,9,5,1,2,7,6};
int C[3][3]={2,9,4,7,5,3,6,1,8};
int D[3][3]={6,7,2,1,5,9,8,3,4};
int E[3][3]={6,1,8,7,5,3,2,9,4};
int F[3][3]={8,3,4,1,5,9,6,7,2};
int H[3][3]={4,9,2,3,5,7,8,1,6};
int I[3][3]={2,7,6,9,5,1,4,3,8};
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>G[i][j];
for(i=0;i<8;i++)
sum[i]=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
sum[0]=sum[0]+abs(A[i][j]-G[i][j]);
sum[1]=sum[1]+abs(B[i][j]-G[i][j]);
sum[2]=sum[2]+abs(C[i][j]-G[i][j]);
sum[3]=sum[3]+abs(D[i][j]-G[i][j]);
sum[4]=sum[4]+abs(E[i][j]-G[i][j]);
sum[5]=sum[5]+abs(F[i][j]-G[i][j]);
sum[6]=sum[6]+abs(H[i][j]-G[i][j]);
sum[7]=sum[7]+abs(I[i][j]-G[i][j]);
}
min=sum[0];
for(i=1;i<8;i++)
if(min>sum[i])
min=sum[i];
cout<<min;
return 0;
}