你已经知道了一个长度为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"); } } }