链接:https://www.nowcoder.com/questionTerminal/c6e414fddd7c401887c350c9cc41f01b?toCommentId=8394574
来源:牛客网
给定任何 N (>=5) 字符的字符串,系统会要求您将字符形成 U 的形状。例如,"helloworld"可以打印为:h d e l l r lowo 也就是说,字符必须按原始顺序打印,从左垂直线开始自上而下,使用 n1 个字符,然后从左到右沿下线与 n2 个字符,最后沿带 n3 个字符的垂直线自下而上。此外,我们希望您尽可能的平方 - 也就是说,必须确信 n1 = n3 = 最大 = k = k <= n2 对于所有 3 <= n2 <\ N = n1 = n2 = n3 = 2 = N。
输入描述:
There are multiple test cases.Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.
输出描述:
For each test case, print the input string in the shape of U as specified in the description.
示例1
输入:
helloworld!
www.nowcoder.com
输出:
h !
e d
l l
lowor
w m
w o
w c
. .
n r
owcode
c语言代码:
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int len,r,u;
int i,j,v,z;
char str[100],result[100][100];
while(gets(str))
{
len=strlen(str);
r=len/3;
u=len%3;
if(u==0)
{
r--;
u=3;
}
for(i=0;i<r+1;i++)
{
for(j=0;j<r+u;j++)
{
result[i][j]=' ';
}
result[i][j]='\0';
}
i=j=0;
for(v=0;v<r;v++)
{
result[v][0]=str[i++];
}
for(j=0;j<r+u;j++)
{
result[r][j]=str[i++];
}
for(z=r-1;z>=0;z--)
{
result[z][j-1]=str[i++];
}
for(i=0;i<r+1;i++)
printf("%s\n",result[i]);
}
return 0;
}