功能:产生 4N 魔方阵。
- 结果
4*4;
16 2 3 13
5 11 7 8
9 10 6 12
4 14 15 1
8*8:
64 2 3 61 60 6 7 57
9 55 11 12 13 51 15 16
17 18 46 20 21 22 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 43 44 45 19 47 48
49 50 14 52 53 54 10 56
8 58 59 5 4 62 63 1
- 源码
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "iomanip"
#define N 8
using namespace std;
int main()
{
// 4N 魔方阵
int i, j;
int square[N + 1][N + 1] = { 0 };
for (j = 1; j <= N; j++)
{
for (i = 1; i <= N; i++)
{
if (j % 4 == i % 4 || (j % 4 + i % 4) == 1)
square[i][j] = (N + 1 - i) * N - j + 1;
else
square[i][j] = (i - 1) * N + j;
}
}
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
cout << setw(5) << square[i][j];
cout << endl;
}
system("pause");
return 0;
}