• API
• FAQ
• Tools
• Archive
SHARE
TWEET

R

namemkazaza Jan 24th, 2020 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <vector>
3.
4. using namespace std;
5.
6. void OddMagicSquare(vector<vector<int> > &matrix, int n);
7. void DoublyEvenMagicSquare(vector<vector<int> > &matrix, int n);
8. void SinglyEvenMagicSquare(vector<vector<int> > &matrix, int n);
9. void MagicSquare(vector<vector<int> > &matrix, int n);
10. void PrintMagicSquare(vector<vector<int> > &matrix, int n);
11.
12. int main(int argc, char* argv[])
13. {
14.   int n;
15.   cin >> n;
16.
17.   vector<vector<int> > matrix(n, vector<int> (n, 0));
18.
19.   if (n<3)
20.   {
21.     return -1;
22.   }
23.
24.   MagicSquare(matrix, n);
25.
26.   int a[100][100];
27.   for (int i = 0; i<n; i++)
28.   {
29.       for (int j = 0; j < n; j++)
30.           a[i][j] = matrix[i][j];
31.   }
32.   for (int i = 0; i<n; i++)
33.   {
34.       for (int j = 0; j < n; j++)
35.           cout << a[i][j] << " ";
36.       cout << "\n";
37.   }
38.   return 0;
39. }
40.
41. void MagicSquare(vector<vector<int> > &matrix,int n)
42. {
43.   if (n%2==1)        //n is Odd
44.     OddMagicSquare(matrix, n);
45.   else          //n is even
46.     if (n%4==0)    //doubly even order
47.       DoublyEvenMagicSquare(matrix, n);
48.     else      //singly even order
49.       SinglyEvenMagicSquare(matrix, n);
50. }
51.
52. void OddMagicSquare(vector<vector<int> > &matrix, int n)
53. {
54.   int nsqr = n * n;
55.   int i=0, j=n/2;     // start position
56.
57.   for (int k=1; k<=nsqr; ++k)
58.   {
59.     matrix[i][j] = k;
60.
61.     i--;
62.     j++;
63.
64.     if (k%n == 0)
65.     {
66.       i += 2;
67.       --j;
68.     }
69.     else
70.     {
71.       if (j==n)
72.         j -= n;
73.       else if (i<0)
74.         i += n;
75.     }
76.   }
77. }
78.
79. void DoublyEvenMagicSquare(vector<vector<int> > &matrix, int n)
80. {
81.   vector<vector<int> > I(n, vector<int> (n, 0));
82.   vector<vector<int> > J(n, vector<int> (n, 0));
83.
84.   int i, j;
85.
86.   //prepare I, J
87.   int index=1;
88.   for (i=0; i<n; i++)
89.     for (j=0; j<n; j++)
90.     {
91.       I[i][j]=((i+1)%4)/2;
92.       J[j][i]=((i+1)%4)/2;
93.       matrix[i][j]=index;
94.       index++;
95.     }
96.
97.   for (i=0; i<n; i++)
98.     for (j=0; j<n; j++)
99.     {
100.       if (I[i][j]==J[i][j])
101.         matrix[i][j]=n*n+1-matrix[i][j];
102.     }
103. }
104.
105. void SinglyEvenMagicSquare(vector<vector<int> > &matrix, int n)
106. {
107.   int p=n/2;
108.
109.   vector<vector<int> > M(p, vector<int> (p, 0));
110.   MagicSquare(M, p);
111.
112.   int i, j, k;
113.
114.   for (i=0; i<p; i++)
115.     for (j=0; j<p; j++)
116.     {
117.       matrix[i][j]=M[i][j];
118.       matrix[i+p][j]=M[i][j]+3*p*p;
119.       matrix[i][j+p]=M[i][j]+2*p*p;
120.       matrix[i+p][j+p]=M[i][j]+p*p;
121.     }
122.
123.   if (n==2)
124.     return;
125.
126.   vector<int> I(p, 0);
127.   vector<int> J;
128.
129.   for (i=0; i<p; i++)
130.     I[i]=i+1;
131.
132.   k=(n-2)/4;
133.
134.   for (i=1; i<=k; i++)
135.     J.push_back(i);
136.
137.   for (i=n-k+2; i<=n; i++)
138.     J.push_back(i);
139.
140.   int temp;
141.   for (i=1; i<=p; i++)
142.     for (j=1; j<=J.size(); j++)
143.     {
144.       temp=matrix[i-1][J[j-1]-1];
145.       matrix[i-1][J[j-1]-1]=matrix[i+p-1][J[j-1]-1];
146.       matrix[i+p-1][J[j-1]-1]=temp;
147.     }
148.
149.   //j=1, i
150.   //i=k+1, k+1+p
151.   i=k;
152.   j=0;
153.   temp=matrix[i][j]; matrix[i][j]=matrix[i+p][j]; matrix[i+p][j]=temp;
154.
155.   j=i;
156.   temp=matrix[i+p][j]; matrix[i+p][j]=matrix[i][j]; matrix[i][j]=temp;
157. }
158.
159.
160. void PrintMagicSquare(vector<vector<int> > &matrix, int n)
161. {
162.   for (int i=0; i<n; i++)
163.   {
164.       for (int j = 0; j < n; j++)
165.           cout << matrix[i][j] << " ";
166.     cout << ("\n");
167.   }
168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top