题目描述
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, "helloworld" can be printed as: h d e l l r lowo That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with 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解答:
根据题目列出关系式:n1+n2+n3-2=N和n1=n3<=n2(且n1和n3要尽可能大),则n1和n3最大极为和n2一样,则n1=n3=(N+2)/3,随即可得到n2的长度
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { int n1,n2,n3; int length = 0; char str[80]; while(scanf("%s", str)!=EOF){ length = strlen(str); n1 = n3 = (length+2)/3; n2 = length+2-n1-n3; for(int i=0;i<n1-1;i++){ printf("%c", str[i]); for(int j=0;j<n2-2;j++) printf(" "); printf("%c\n",str[length-i-1]); } for(int i=0;i<n2;i++){ printf("%c", str[length-n3-n2+i+1]); } printf("\n"); } return 0; }
牛客网--Hello World For U
猜你喜欢
转载自blog.csdn.net/jackson_j/article/details/114400850
今日推荐
周排行