描述
你已經知道了一個長度為R(R<=100)的字串,現在要求你按照如下方式填入一個N*N的方陣中,如果填不滿,剩下的位置補充為’#’。
字串:hello_c++
填充到一個3*3的方陣中效果:
hel
++l
c_o
字串:where_me
填充到3*3的方陣中效果:
whe
e#r
m_e
(沒有填充滿用#代替)
-
輸入
-
第一行包含一個整數T,表示有T組測試資料。
以下每組測試資料格式:
第一行包含2個整數R和N,其中R表示字串長度,N代表方陣大小,小於等於10.
第二行包含一個含有R個字元的字串,字元集:’a’-‘z’,’A’-‘Z’,’0’-‘9’,空格以及常用標點符號。
-
輸出
-
輸出填充好後的方陣。
類比題,找規律
#include <stdio.h>#include <string.h>main(){int number,t;char a[12][12];int m,n;char r[101];int i,j,k;int count;int up;scanf("%d",&number);for(t=1;t<=number;t++){scanf("%d %d",&m,&n);scanf("%s",&r);memset(a,'#',sizeof(a));count=m;k=0;up=0;while(1){for(i=k+1;i<=n-k;i++,up++){a[k][i]=r[up];count--;if(count==0)goto ABC;}for(i=k+1;i<n-k;i++,up++){a[i][n-k]=r[up];count--;if(count==0)goto ABC;}for(j=n-k-1;j>=k+1;j--,up++){a[n-k-1][j]=r[up];count--;if(count==0)goto ABC;}for(j=n-k-1-1;j>=k+1;j--,up++){a[j][k+1]=r[up];count--;if(count==0)goto ABC;}k++;}ABC:for(i=0;i<n;i++){for(j=1;j<=n;j++)printf("%c",a[i][j]);printf("\n");}}}