1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11
来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/28 16:20:23
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 5
int 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
运行环境:
g++
#include
#include
using namespace std;
#define ARRAYSIZE 5
int 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
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11
4 3 5 6 8 9 7 11 10 12 13 14 15 16 18 17 20 21 22 19 24 23 2
1+2+3+4+5+6+7+8+9+10+11+14+15+16+17+18+19+20+21+22+23+24+25+
1,2,3,4,5,6,7,8,9,1o,11,12,13,14,15,16,17,18,19,20,21,22,23,
将25、24、23、22、21、20、19、18、17、16、15、14、13、12、11、10、9、8、7、6、5、4
1+2+3+4+5+6+7+8+9+10+11++12+13+14+15+16+17+18+19+20+21+22+23
1+2+3-4+5+6-7-8+9+10-11-12+13+14-15-16+17+18-19-20+21+22-23-
1、2、3、4、5、6、7、8、9、10、11、12、13、14、15、16、17、18、19、20、21、22、23、
在1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20*21*22*23*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23