螺旋矩阵,直接模拟就可以了。
#include <stdio.h>
#include <string.h>
int row,col;
char data[25][25];
char str[500];
char text[500];
int main()
{
int t,i,j,k,l,smallnum,ctr;
char c;
int temprow,tempcol;
int test=1;
int num;
scanf( "%d",&t );
while ( t-- ) {
scanf( "%d%d",&row,&col );
if ( row>col )
smallnum=col;
else
smallnum=row;
ctr=smallnum/2;
getchar();
gets(text);
l=strlen(text);
for ( i=0;i<l;i++ ) {
if ( text[i]==' ' )
num=0;
else
num=text[i]+1-'A';
str[i*5]='0';
str[i*5+1]='0';
str[i*5+2]='0';
str[i*5+3]='0';
str[i*5+4]='0';
k=4;
while ( num!=0 ) {
str[i*5+k]=num%2+'0';
k--;
num=num/2;
}
}
for ( k=5*l;k<row*col;k++ )
str[k]='0';
k=0;
for ( i=0;i<ctr;i++ ) {
temprow=row-1-i;
tempcol=col-1-i;
for ( j=i;j<tempcol;j++ ) {
data[i][j]=str[k];
k++;
}
for ( j=i;j<temprow;j++ ) {
data[j][tempcol]=str[k];
k++;
}
for ( j=tempcol;j>i;j-- ) {
data[temprow][j]=str[k];
k++;
}
for ( j=temprow;j>i;j-- ) {
data[j][i]=str[k];
k++;
}
}
if ( smallnum&1 ) {
i=ctr;
if ( row<=col ) {
for ( j=i; j<col-i;j++ ) {
data[i][j]=str[k];
k++;
}
}
else {
for ( j=i;j<row-i;j++ ) {
data[j][i]=str[k];
k++;
}
}
}
printf( "%d ",test );
test++;
for ( i=0;i<row;i++ )
for ( j=0;j<col;j++ )
printf( "%c",data[i][j] );
printf( "\n" );
}
return 0;
}