看过网上的一些方法,觉得思路很麻烦,所以自己写了一个。这个是满分答案,思路很简单。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf( "%d", &n );
int **a;
a = (int**)malloc(sizeof(int*) * n);
for( int i = 0; i < n; i++ )
a[i] = (int*)malloc(sizeof(int) * n);
for( int i = 0; i < n; i++ )
for( int j = 0; j < n; j++ )
scanf( "%d", &a[i][j] );
for( int i = 0; i < n; i++ )
for( int j = i; j >= 0; j-- )
{
if( i % 2 == 0 )
printf( "%d ", a[j][i - j] );
else
printf( "%d ", a[i - j][j] );
}
int m = n;
for( int i = 1; i < n; i++ )
{
for( int j = i; j < n; j++ )
{
if( n % 2 == 1 )
{
if( i % 2 == 1 )
printf( "%d ", a[j][m - j] );
else
printf( "%d ", a[m - j][j] );
}
else
{
if( i % 2 == 1 )
printf( "%d ", a[m - j][j] );
else
printf( "%d ", a[j][m - j] );
}
}
m++;
}
return 0;
}