请问这题该怎么做
输入可大可小的正方形图案,最外层是第一层,要求每层上用的数字与层数相同。Input n: 3
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
#include <stdio.h> #include <stdlib.h> int main(void) { int i, j, m, n; printf("Input n: "); scanf("%d", &n); for(i = 1 - n; i <= n - 1; i++) { for(j = 1 - n; j <= n - 1; j++) { m = n - abs(j); if(m > n - abs(i)) m = n - abs(i); printf("%d ", m); } printf("\n"); } return 0; }
#include <stdio.h> #define MIN(a,b) ( ((a)<(b)) ? (a) : (b) ) void fun( unsigned n ) { for( unsigned i=0; i!=(2*n-1)*(2*n-1); ++i ) { unsigned r = i/(2*n-1); unsigned c = i%(2*n-1); unsigned v = MIN( MIN(r+1,2*n-1-r), MIN(c+1,2*n-1-c) ); printf( "%d%c", v, " \n"[(i+1)%(2*n-1)==0] ); } } int main( void ) { fun( 3 ); return 0; }