求高手帮忙看下这个code 错在哪?
问题是有个矩阵1 1
1 -1
然后变成
1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1
在一点点按规律变大
#include <stdio.h>
#include <stdlib.h>
void fill( int *A, int n, int r, int c, int sign ) ;
int main( int argc, char *argv[] ) {
int n, i, j, sign ;
int *A ;
n = atoi( argv[1] ) ;
A = ( int * ) malloc( sizeof( int ) * n * n ) ;
sign = 1 ;
fill( A, n, 0, 0, sign ) ;
printf( "\n") ;
for ( i = 0 ; i < n ; i++ ) {
for ( j = 0 ; j < n ; j++ ) {
printf( "%5d", A[ i*n+j ] ) ;
}
printf( "\n" ) ;
}
return 0;
}
void fill( int *A, int n, int r, int c, int sign ) {
if ( n == 2 ) {
A[ (r*n) + c ] = sign ;
A[ (r+1)*n + c ] = sign ;
A[ (r*n) + (c+1) ] = sign ;
A[ (r+1)*n + (c+1) ] = -sign ;
}
else {
fill( A, n, r, c, sign ) ;
fill( A, n, r, c+n/2, sign ) ;
fill( A, n, r+n/2, c, sign ) ;
fill( A, n, r+n/2, c+n/2, sign * (-1) ) ;
}
}