PAT 1031. Hello World for U (20)

Source: Internet
Author: User

http://pat.zju.edu.cn/contests/pat-a-practise/1031

Given any string of N (>=5) characters, and you are asked to form the characters into the shape of U. For example, the "HelloWorld" can be printed as:

H  d
e  l
l  R
Lowo
This is, the characters must are printed in the original order, starting Top-down to the left vertical line with N 1 Char Acters, then left to right along the bottom line with n 2 characters, and finally bottom-up the along line with N 3 characters. And more, we would like you to being as squared as possible--that's, it must being satisfied that n 1 = n 3 = max {k| K < = n 2 for all 3 <= N 2 <= n} with n 1 + N 2 + n 3-2 = N.

Input Specification:

Each input file contains one test case. Each case is contains one string with no less than 5 and no more than-characters in a line. The string contains no white spaces.

Output Specification:

For each test case, print the input string in the shape of U as specified in the description. Sample Input:

helloworld!
Sample Output:
H   !
E   D
l   L
lowor
#include <cstdio>
#include <cstring>

int main () {
	char s[81];
	scanf ("%s", s);
	int n = strlen (s);
	int a, B;
	int max =-1;
	for (int j = 3; J <= N; ++j) {for
		(int i = 1; I <= J; ++i) {
			if (2 * i + j = = n + 2) {
				if (i > Max) 
  max = i
	}}} A = max;
	b = n + 2-2 * A;
	for (int i = 0; i < A-1 i++) {
		printf ('%c ', s[i]);
		for (int k = 0; k < b-2; k++)
			printf ("");
		printf ("%c\n", S[n-i-1]);
	}
	for (int l = A-1 L < b + A-1; l++)
		printf ("%c", S[l));
	printf ("\ n");
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.