C语言 求解 二维数组的回形遍历

#include <stdio.h>
#define ROW 100
#define COL 100
int main(){
int row, col, matrix[ROW][COL];
int xx_row, sx_row, xx_col, sx_col;
int i, j;

scanf("%d %d", &row, &col);
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
scanf("%d",&(matrix[i][j]));
}
}
xx_row = 0;
sx_row = row-1;
xx_col = 0;
sx_col = col-1;

while( (xx_row <sx_row) || (xx_row<sx_row) ){
for(j = xx_col; j < sx_col; j++){
printf("%d\n", matrix[xx_row][j]);
}

for(i = xx_row; i < sx_row; i++){
printf("%d\n", matrix[i][sx_col]);
}

for(j = sx_col; j > xx_col; j--){
printf("%d\n", matrix[sx_row][j]);
}

for(i = sx_row; i > xx_row; i--){
printf("%d\n", matrix[i][xx_col]);
}
xx_row++;
sx_row--;
xx_col ++;
sx_col --;

}
system("pause");
return 0;
}
对于图片中的例子中间的不能输出,要怎么改

你难道想实现这样的效果?

 

还是想实现这样的效果?

 

以下代码实现的是第一种效果:

 

#include <stdio.h>

int main()

{

 #define ROW 100 

 int matrix[ROW][ROW]={0};

 int i,j;

 

 int row,col,tol;

 int left,top,right,bottom;

 

 row=5;col=5;

 for(i=0;i<row;i++)

 {

  for(j=0;j<col;j++)

  {

   matrix[i][j]=i*col+j;

  }

 }

 

 ///////////////////////////////

 // printf("请输入行和列,如(5 5):");

 // fflush(stdin);scanf("%d %d", &row, &col);

 // if(row > ROW || col >ROW) 

 // {

  // printf("别输入太大的数,最大只能100\n");

  // return 0;

 // }

 // printf("那么请逐个输入数据吧\n");

  // for(i = 0; i < row; i++)

  // { 

   // for(j = 0; j < col; j++)

   // { 

   // printf("数据%2d:",i*col+j);

   // fflush(stdin);scanf("%d",&(matrix[i][j])); 

   // } 

  // }

 ////////////////////////////

 

 for(i=0;i<row;i++)

 {

  for(j=0;j<col;j++)

  {

   printf("%3d ",matrix[i][j]);

  }

  printf("\n");

 }

 printf("\n===结果=====:\n");

 tol=0;

 left=0;top=0;

 right=col;bottom=row;

 

 // top

 if(left<right)

 {

  for(j=left;j<right;j++)

  {

   tol++;

   printf("%3d ",matrix[top][j]);

  }

  printf("\n");

 }

 

 for(i=1;i<row-1;i++)

 {

  for(j=0;j<col;j++)

  {

   // left

   if(j==0)

   {

    printf("%3d ",matrix[i][j]);

   }

   // right

   else if(j==col-1)

   {

    printf("%3d ",matrix[i][j]);

   }

   // 中间不输出是吧。。。那以空格代替好了

   else

   {

    printf("    ",matrix[i][j]);

   }

  }

  printf("\n");

 }

 // bottom

 if(top+1 != bottom)

 {

  for(j=right;--j>left-1;)

  {

   tol++;

   printf("%3d ",matrix[bottom-1][j]);

  }

  printf("\n");

 }

 return 0;

}

追问

例子输入

4 4 1 2 3 412 13 14 511 16 15 610 9 8 7

例子输出

12345678910111213141516

但是这个只能对于行数和列数相同的可以,如果行数和列数不相同怎么办

追答

还有这种情况?那我上机看看我的代码的执行:

 

===以下是 5行 8列的结果,输出也正常啊。。。

 

不信,你其实可以复制我写的代码,上机试试的。

(怎么有写好的代码你不复制,而要一个字一个字地输入?)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-12-27
#include <stdio.h>
#define ROW 100
#define COL 100
int main(){
 int row, col, matrix[ROW][COL];
 int xx_row, sx_row, xx_col, sx_col;
 int i, j;
 
 scanf("%d %d", &row, &col);
 for(i = 0; i < row; i++){
 for(j = 0; j < col; j++){
 scanf("%d",&(matrix[i][j]));
 }
 }
    xx_row = 0;
    sx_row = row-1;
    xx_col  = 0;
    sx_col = col-1;
 

    while( 1 ){
for(j = xx_col; j <= sx_col; j++){
 printf("%d\n", matrix[xx_row][j]);
}
xx_row++;
if (xx_row>sx_row)
 break;
for(i = xx_row; i <= sx_row; i++){
 printf("%d\n", matrix[i][sx_col]);
}

sx_col--;
if (xx_row>sx_col)
 break;
for(j = sx_col; j >= xx_col; j--){
 printf("%d\n", matrix[sx_row][j]);
}

sx_row--;
if (xx_row>sx_row)
 break;
for(i = sx_row; i >= xx_row; i--){
 printf("%d\n", matrix[i][xx_col]);
}

   xx_col ++;
if (xx_col>sx_col)
 break;
    }
system("pause");
 return 0;
}

第2个回答  2013-12-27
简单嘛,输出的时候判断行数,如果为第一行和第三行,则全部显示,如果是第二行 ,则显示第一个和最后一个,其他都输出空格。追问

不单单是这一个例子的,就是这个程序只对于行数和列数相同的可以正常输出,如果不相同就会有一部分数据没有输出

相似回答