C语言 二维数组回形遍历 50
题目要求:给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按回形从外向内顺时针顺序遍历整个数组。如图所示:http://f.hipho...
题目要求:给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按回形从外向内顺时针顺序遍历整个数组。如图所示:http://f.hiphotos.baidu.com/zhidao/pic/item/95eef01f3a292df5e5ab0b0abc315c6034a87340.jpg
我的程序:
#include<stdio.h>int main(){ int row,col,i,j,g,k,p,num,time; int sz[110][110]; scanf("%d %d",&row,&col); num=row*col; for(i=0;i<row;i++){ for(j=0;j<col;j++){ scanf("%d",&sz[i][j]); }} for(i=0;;i++){ for(j=i;j<col-i;j++){ printf("%d\n",sz[i][j]);time++;} if(time==num){break;} for(g=1+i;g<row-i;g++){ printf("%d\n",sz[g][col-1-i]);time++;} if(time==num){break;} for(k=col-2-i;k>=i;k--){ printf("%d\n",sz[row-1-i][k]);time++;} if(time==num){break;} for(p=row-2-i;p>i;p--){ printf("%d\n",sz[p][i]);time++;} if(time==num){break;} }}
提交显示错误,错误提示:
ProgramError: Case 0. Time out.Error 1 in case 0.Empty output file: '0.out'
求高手指点!!!谢谢! 展开
我的程序:
#include<stdio.h>int main(){ int row,col,i,j,g,k,p,num,time; int sz[110][110]; scanf("%d %d",&row,&col); num=row*col; for(i=0;i<row;i++){ for(j=0;j<col;j++){ scanf("%d",&sz[i][j]); }} for(i=0;;i++){ for(j=i;j<col-i;j++){ printf("%d\n",sz[i][j]);time++;} if(time==num){break;} for(g=1+i;g<row-i;g++){ printf("%d\n",sz[g][col-1-i]);time++;} if(time==num){break;} for(k=col-2-i;k>=i;k--){ printf("%d\n",sz[row-1-i][k]);time++;} if(time==num){break;} for(p=row-2-i;p>i;p--){ printf("%d\n",sz[p][i]);time++;} if(time==num){break;} }}
提交显示错误,错误提示:
ProgramError: Case 0. Time out.Error 1 in case 0.Empty output file: '0.out'
求高手指点!!!谢谢! 展开
2个回答
展开全部
#include <stdio.h>
#define N 20
int main( void )
{
int row, col, i, j, k, i1, j1;
int num;
int array[N][N];
printf( "Please input row and col:\n");
scanf( "%d %d", &row, &col );
num = row * col;
for( i = 0; i < row; i++ )
for( j = 0; j < col; j++ )
scanf( "%d", &array[i][j] );
for( i = 0; ; i++ ){
for( j = i; j < col - i; j++ ){
printf( "%d\t", array[i][j] );
num--;
if( num == 0 )
return 0;
}
for( k = i + 1; k < row - i; k++ ){
printf( "%d\t", array[k][j-1]);
num--;
if( num == 0 )
return 0;
}
for( i1 = j - 2; i1 > i - 1; i1-- ){
printf( "%d\t", array[k-1][i1] );
num--;
if( num == 0 )
return 0;
}
for( j1 = k - 2; j1 > i; j1-- ){
printf( "%d\t", array[j1][i] );
num--;
if( num == 0 )
return 0;
}
}
return 0;
}
#define N 20
int main( void )
{
int row, col, i, j, k, i1, j1;
int num;
int array[N][N];
printf( "Please input row and col:\n");
scanf( "%d %d", &row, &col );
num = row * col;
for( i = 0; i < row; i++ )
for( j = 0; j < col; j++ )
scanf( "%d", &array[i][j] );
for( i = 0; ; i++ ){
for( j = i; j < col - i; j++ ){
printf( "%d\t", array[i][j] );
num--;
if( num == 0 )
return 0;
}
for( k = i + 1; k < row - i; k++ ){
printf( "%d\t", array[k][j-1]);
num--;
if( num == 0 )
return 0;
}
for( i1 = j - 2; i1 > i - 1; i1-- ){
printf( "%d\t", array[k-1][i1] );
num--;
if( num == 0 )
return 0;
}
for( j1 = k - 2; j1 > i; j1-- ){
printf( "%d\t", array[j1][i] );
num--;
if( num == 0 )
return 0;
}
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询