#include<iostream>usingnamespacestd;
int main()
{
constint NUM = 5;
if (NUM % 2 == 0)
{
cerr << "不可以为偶数!" << endl;
exit(EXIT_FAILURE);
}
int x = 0,
y = NUM / 2;
int square[NUM][NUM] = { 0 };
square[x][y] = 1;
for (int i = 2; i <= NUM * NUM; i++)
{
x--;
y++;
if (x < 0 && y >= NUM)
{
x += 2;
y -= 1;
}
if (x < 0)
x = NUM - 1;
if (y >= NUM)
y = 0;
while (square[x][y] != 0)
{
x = x + 2;
y = y - 1;
if (x >= NUM)
x = 0;
if (y < 0)
y = NUM - 1;
}
square[x][y] = i;
}
for (int i = 0; i < NUM; i++)
{
for (int j = 0; j < NUM; j++)
cout << square[i][j] << "\t";
cout << endl;
}
return0;
}
书上的一道题目,魔方矩阵,一行的元素相加与一列的元素相加的和一样大,对角线也一样大一开始不会写,百度也没找到。。后来自己慢慢单步调试写出来了。。。 **仅供有需要的人以参考,如有错误请纠正我**#include<iostream>using namespace std;int main(){ const int N