#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
int n;
cout << "請輸入方陣階數:";
cin >> n;
int A[n][n];
int r = 0;
int c = (n-1)/2;
int v = 1;
for(int i = 0; i < n; i++)
{
for(int j =0; j<n; j++)
{
A[i][j] = -1;
}
}
while(v<=(n*n))
{
A[r][c] = v;
v++;
r--;
c++;
if((r < 0)&&(c <= n-1))
{
r = n-1;
}
else if ((r>=0) && (c > n-1))
{
c = 0;
}
else if((r < 0)&&(c > (n-1)))
{
r = r+2;
c--;
}
else if(A[r][c]!= -1)
{
r = r+2;
c--;
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << A[i][j] << "\t";
}
cout << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
沒有留言:
張貼留言