回复 2楼 rjsp
这个问题,有几年了,在网上查找过方法。奇数,好计算。
偶数,没办法。
这个问题有几年了,没找到答案。。。
最近,在温习之前学过的编程方法和思路。。。
于是,又想起了这个问题。。。。
[此贴子已经被作者于2022-7-8 23:51编辑过]
char number[4][36]={{ 6, 1, 8, 7, 5, 3, 2, 9, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 1, 2,15,16,12,14, 3, 5,13, 7,10, 4, 8,11, 6, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {14, 9, 1,19,22,23,18, 2,16, 6,21,11,13,15, 5, 3,10,24, 8,20, 4,17,25, 7,12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 1, 5,34,27,18,26, 7,32, 9,21,22,20,30, 2,31,16,15,17, 4,29, 6,24,25,23,36, 8,28,13,12,14,33,35, 3,10,19,11}}; void num_out(char num) { for(char i=0; i<num; i++) { for(char j=0; j<num; j++) printf("%3d", number[num-3][i*num+j]); printf("\n"); } }
#include <stdio.h> #define N 5 //3, 5, 7, 9 行、列数 int main() { int a[N][N] = {0}; int count =1 ; int row = 0, cul = N / 2; while (count <= N*N) { a[row][cul] = count; int i = row; int j = cul; if (i == 0) { i = N - 1; } else { i--; } j = (j +1) % N; if (a[i][j]!=0||(row==0&&cul==N-1)) { i = row + 1; j = cul; } row = i; cul = j; count++; } for (int i =0 ; i < N; i++) { for (int j =0 ; j < N; j++) { printf("%3d",a[i][j]); } printf("\n"); } }
[此贴子已经被作者于2022-7-9 02:40编辑过]
#include <iostream> char number[8][8]={{ 1, 3,64,61,62, 4,63, 2}, {60,57, 5, 8, 7,59, 6,58}, {17,19,48,46,20,45,18,47}, {51,16,14,49,50,15,13,52}, {25,38,40,26,37,28,27,39}, {21,42,44,24,41,23,43,22}, {29,31,36,34,32,33,35,30}, {56,54, 9,12,11,53,55,10}}; void num_out() { for(char i=0; i<8; i++) { for(char j=0; j<8; j++) printf("%3d", number[i][j]); printf("\n"); } } int main() { num_out(); return 0; }
[此贴子已经被作者于2022-7-9 01:07编辑过]