救救孩子
目前只学了数组之前的基本知识指针只限于知道 还没学到数组与指针那张
这是作业题 求大佬们用基础的语句解答下
define a m×n array ,output the array with the following type using 1-m*n
输入 3 4
输出
1 2 3 4
10 11 12 5
9 8 7 6
#include <stdio.h> void foo( unsigned row, unsigned col ) { for( unsigned i=0; i!=row*col; ++i ) { unsigned r = i/col; unsigned c = i%col; unsigned v; if( r-0<=row-1-r && r-0<=c-0 && r-0<=col-1-c ) // → v = 2*(row+col-2*r)*r + c-r+1; else if( col-1-c<=c-0 && col-1-c<=r-0 && col-1-c<=row-1-r ) // ↓ v = 2*(row+col-2*(col-1-c))*(col-1-c) + r+3*c-2*col+3; else if( row-1-r<=r-0 && row-1-r<=col-1-c && row-1-r<=c-0 ) // ← v = 2*(row+col-2*(row-1-r))*(row-1-r) + 2*col-4*row+5*r-c+3; else if( c-0<=col-1-c && c-0<=row-1-r && c-0<=r-0 ) // ↑ v = 2*(row+col-2*c)*c + 2*row+2*col-r-7*c-3; printf( "%u%c", v, " \n"[c+1==col] ); } } int main( void ) { unsigned m, n; scanf( "%u%u", &m, &n ); foo( m, n ); }
#include <stdio.h> void foo( unsigned row, unsigned col, unsigned base, const char* fmt ) { for( unsigned i=0; i!=row*col; ++i ) { unsigned T = i/col; unsigned B = row-1-T; unsigned L = i%col; unsigned R = col-1-L; unsigned v; #define bar(row,col,t,l,base) (2*(row)*(t) + 2*(col)*(t) - 4*(t)*(t) + (l)-(t) + (base)) if( T<=B && T<=L && T<=R ) // → v = bar( row, col, T, L, base ); else if( R<=T && R<=B && R<=L ) // ↓ v = bar( col, row-1, R, T-1, base+col ); else if( B<=T && B<=L && B<=R ) // ← v = bar( row-1, col-1, B, R-1, base+col+row-1 ); else if( L<=T && L<=B && L<=R ) // ↑ v = bar( col-1, row-2, L, B-1, base+2*col+row-2 ); #undef bar printf( fmt, v ); putchar( " \n"[R==0] ); } } int main( void ) { foo( 5, 4, 1, "%02u" ); foo( 5, 5, 1, "%02u" ); foo( 5, 6, 1, "%02u" ); foo( 7, 11, 1, "%02u" ); foo( 17, 9, 1, "%03u" ); }