1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11

学习 时间:2026-03-30 09:03:55 阅读:7633
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9怎样用数组实现

最佳回答

超帅的美女

甜美的服饰

2026-03-30 09:03:55

代码有点丑陋,但能够解决你需要的问题:运行环境:g++#include #include using namespace std;#define ARRAYSIZE 5int array[ARRAYSIZE][ARRAYSIZE] = {0};void setArray(){int r = 0;int c = 0;int cnt = 1;int dir = 0; // 0:右;1:下;2:左; 3:上while( true ) {if(cnt > ARRAYSIZE*ARRAYSIZE)break;if( r >= 0 && r < ARRAYSIZE&& c >= 0 && c < ARRAYSIZE&& array[r][c] ==0 ) {array[r][c] = cnt;cnt ++;}else {switch(dir) {case 0:c --;break;case 1:r --;break;case 2:c ++;break;case 3:r ++;break;}dir ++;dir %=4;}switch(dir) {case 0:c ++;break;case 1:r ++;break;case 2:c --;break;case 3:r --;break;}}}void output(){for(int i=0; i < ARRAYSIZE; ++i ) {for(int j=0; j < ARRAYSIZE; ++j ) {cout

最新回答共有2条回答

  • 瘦瘦的大雁
    回复
    2026-03-30 09:03:55

    代码有点丑陋,但能够解决你需要的问题:运行环境:g++#include #include using namespace std;#define ARRAYSIZE 5int array[ARRAYSIZE][ARRAYSIZE] = {0};void setArray(){int r = 0;int c = 0;int cnt = 1;int dir = 0; // 0:右;1:下;2:左; 3:上while( true ) {if(cnt > ARRAYSIZE*ARRAYSIZE)break;if( r >= 0 && r = 0 && c < ARRAYSIZE&& array[r][c] ==0 ) {array[r][c] = cnt;cnt ++;}else {switch(dir) {case 0:c --;break;case 1:r --;break;case 2:c ++;break;case 3:r ++;break;}dir ++;dir %=4;}switch(dir) {case 0:c ++;break;case 1:r ++;break;case 2:c --;break;case 3:r --;break;}}}void output(){for(int i=0; i < ARRAYSIZE; ++i ) {for(int j=0; j < ARRAYSIZE; ++j ) {cout

上一篇 英语翻译Nor need pleasure be gross and sensual.there are no game

下一篇 细胞核的作用是