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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <vector>
#include<iostream>
using namespace std;
void OddMagicSquare(vector<vector<int> > &matrix, int n);
void DoublyEvenMagicSquare(vector<vector<int> > &matrix, int n);
void SinglyEvenMagicSquare(vector<vector<int> > &matrix, int n);
void MagicSquare(vector<vector<int> > &matrix, int n);
void PrintMagicSquare(vector<vector<int> > &matrix, int n);
int main(int argc, char* argv[])
{
int n;
char again=0;
do{
cout<<"\t\t\tEnter order of square: ";
scanf("%d", &n);
while (n<3 || n>15)
{
cout<<"\nError: n must be greater than 2 and less than 16:";
cin.clear();
fflush(stdin);
cin>>n;
}
cout<<"\t\t ===============================\n\n"<< endl;
vector<vector<int> > matrix(n, vector<int> (n, 0));
MagicSquare(matrix, n);
//Print results
PrintMagicSquare(matrix, n);
cout << " Do you want to continue? (y/n)";
cin>>again;
}while( again=='Y' || again=='y');
system("pause");
return 0;
}
void MagicSquare(vector<vector<int> > &matrix,int n)
{
if (n%2==1) //n is Odd
OddMagicSquare(matrix, n);
else //n is even
if (n%4==0) //doubly even order
DoublyEvenMagicSquare(matrix, n);
else //singly even order
SinglyEvenMagicSquare(matrix, n);
}
void OddMagicSquare(vector<vector<int> > &matrix, int n)
{
int nsqr = n * n;
int i=0, j=n/2; // start position
for (int k=1; k<=nsqr; ++k)
{
matrix[i][j] = k;
i--;
j++;
if (k%n == 0)
{
i += 2;
--j;
}
else
{
if (j==n)
j -= n;
else if (i<0)
i += n;
}
}
}
void DoublyEvenMagicSquare(vector<vector<int> > &matrix, int n)
{
vector<vector<int> > I(n, vector<int> (n, 0));
vector<vector<int> > J(n, vector<int> (n, 0));
int i, j;
//prepare I, J
int index=1;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
I[i][j]=((i+1)%4)/2;
J[j][i]=((i+1)%4)/2;
matrix[i][j]=index;
index++;
}
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
if (I[i][j]==J[i][j])
matrix[i][j]=n*n+1-matrix[i][j];
}
}
void SinglyEvenMagicSquare(vector<vector<int> > &matrix, int n)
{
int p=n/2;
vector<vector<int> > M(p, vector<int> (p, 0));
MagicSquare(M, p);
int i, j, k;
for (i=0; i<p; i++)
for (j=0; j<p; j++)
{
matrix[i][j]=M[i][j];
matrix[i+p][j]=M[i][j]+3*p*p;
matrix[i][j+p]=M[i][j]+2*p*p;
matrix[i+p][j+p]=M[i][j]+p*p;
}
if (n==2)
return;
vector<int> I(p, 0);
vector<int> J;
for (i=0; i<p; i++)
I[i]=i+1;
k=(n-2)/4;
for (i=1; i<=k; i++)
J.push_back(i);
for (i=n-k+2; i<=n; i++)
J.push_back(i);
int temp;
for (i=1; i<=p; i++)
for (j=1; j<=J.size(); j++)
{
temp=matrix[i-1][J[j-1]-1];
matrix[i-1][J[j-1]-1]=matrix[i+p-1][J[j-1]-1];
matrix[i+p-1][J[j-1]-1]=temp;
}
//j=1, i
//i=k+1, k+1+p
i=k;
j=0;
temp=matrix[i][j]; matrix[i][j]=matrix[i+p][j]; matrix[i+p][j]=temp;
j=i;
temp=matrix[i+p][j]; matrix[i+p][j]=matrix[i][j]; matrix[i][j]=temp;
}
void PrintMagicSquare(vector<vector<int> > &matrix, int n)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf(" |%3d ", matrix[i][j]);
cout<<"|\n\n";
}
cout<<"\n\n";
}
|