1358 螺旋文字陣

來源:互聯網
上載者:User
 
描述

你已經知道了一個長度為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");}}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.