text1

来源:互联网 发布:内网软件 编辑:程序博客网 时间:2024/06/05 04:01
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
int a[50][50];
cout << "输入行数(需为奇数)" << endl;
int x;
int i,j;
cin >> x;
if(x % 2 == 0)
{
cout << "error" << endl;
return 0;

for(i = 0; i < x; i++)
{
for(j = 0; j < x; j++)
{
a[i][j] = 0;
}
}
i = 0;
j = x/2;
a[i][j] = 1;
int p,q;
for(int t = 2; t < x*x; t++)
{
//int p,q;
p = i;
q = j;
i--;
j++;
if(i < 0)
{
i = x - 1;
}
if(j >= x)
{
j = 0;
}
if(a[i][j] != 0)
{
i = p + 1;
j = q;
}
a[i][j] = t;
}
for(int i = 0; i < x; i++)
{
for(int j = 0; j < x; j++)
{
cout << setw(4) << a[i][j];

cout << endl;
}
return 0;
}
原创粉丝点击