好像小学数学上有n次方的方法,以下是我以前写的一个
<code>
#include<stdio.h>
int main(){
int f [100] [100] = { 0 };
int a , b , k , n;
scanf("%d",&n);
if ( n <= 0 || n >= 100 || !( n%2 ) ){
printf("wrong number!\n");
return 0;
}
a = 0;
b = n/2;
for ( k = 1 ; k <= n * n ; k++ ){
while ( f [a] [b] != 0 || a < 0 || b > n-1 ){
if ( a < 0 ) {
if ( b > n-1 ) {
a = 1 ;
b -= 1;
}
else a = n-1;
}
else if ( b > n-1 ) b=0;
if ( f[a][b] != 0 ) {
a += 2;
b -= 1;
}
}
f[a][b]=k;
a -= 1;
b += 1;
}
for ( a = 0 ; a <= n - 1 ; a++ ){
for ( b = 0 ; b <= n - 1 ; b++ ) printf( "%-5d" , f [a] [b] );
printf ( "\n\n\n\n" );
}
return 0;
}
</code>