1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11
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怎样用数组实现
最佳回答
代码有点丑陋,但能够解决你需要的问题:运行环境: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
热门文章
- 康达学院专转本五年制
- 高考一个考场分ab卷吗
- not only but also用法
- 某物体做自由落体运动,从释放开始计时,则物体在前2s内的平均速度为______m/s,物体下落2m时的速度大小为______m/s.
- 三角函数公式大全表格
- 地理中考必背知识点2022
- 2013-2014学年小学六年级科学上学期期末考试试卷及答案
- 人教版2014-2015学年小学五年级英语第二学期期中教学质量检测试卷及答案
- 【Linux驱动开发】设备树详解(二)设备树语法详解
- 别跟客户扯细节
- 在别的城市买房子能落户吗
- 卖房前要把装修贷还完吗
- 高中政治教学提高教学效果的方法探究
- “互联网+”背景下的初中英语课堂教学改革与创新策略研究
- 2022年终止合同范本
- 租房合同范本范文
- 如何挑选土豆
- 如何挑选土鸡
