1031 Hello World for U (20分)

Given any string of N (≥) 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 n?1?? characters, then left to right along the bottom line with n?2?? characters, and finally bottom-up along the vertical line with n?3?? characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n?1??=n?3??=max { k | k≤n?2?? for all 3 } with n?1??+n?2??+n?3??−2=N.

Input Specification:

Each input file contains one test case. 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.

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

测试点5没过

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<stack>
 7 #include<algorithm>
 8 #include<string>
 9 #include<cmath>
10 using namespace std;
11 void printBlock(int n)
12 {
13     while (n--)
14         cout << " ";
15 }
16 int main()
17 {
18     string s;
19     cin >> s;
20     int N = s.length() - 2;
21     int n1=0, n2=0;
22     int flag = 0;
23     for (n2 = 3; n2 <= N; n2++)
24     {
25         for (n1=0;n1 <= n2; n1++)
26             if (2 * n1 + n2 == s.length())
27             {
28                 flag = 1;
29                 break;
30             }
31         if (flag)
32             break;
33     }
34     for (int i = 0; i < n1; i++)
35     {
36         cout << s[i];
37         printBlock(n2 - 2);
38         cout << s[s.length() - i - 1] << endl;
39     }
40     cout << s.substr(n1, n2);
41     return 0;
42 }

原文地址:https://www.cnblogs.com/57one/p/12005994.html

时间: 2024-10-26 19:28:59

1031 Hello World for U (20分)的相关文章

1031 Hello World for U (20 分)

1031 Hello World for U (20 分) Given any string of N (≥) 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, s

【PAT甲级】1031 Hello World for U (20 分)

题意: 输入一个字符串长度为5~80,以'U'型输出,使得底端一行字符数量不小于侧面一列,左右两列长度相等. trick: 不把输出的数组全部赋值为空格为全部答案错误,可能不赋值数组里值为0,赋值后是' ',空格的ascii是32,初读题面时并没有看到要输出空格,因为打印0其实效果看起来好像一样... 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;string s;char a[87][87

PAT Advanced 1031 Hello World for U (20分)

Given any string of N (≥) 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

1031 Hello World for U (20 分)

1031 Hello World for U (20 分) 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,

PTA-C-4-7 统计某类完全平方数 (20分)

4-7 统计某类完全平方数   (20分) 本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144.676等. 函数接口定义: int IsTheNumber ( const int N ); 其中N是用户传入的参数.如果N满足条件,则该函数必须返回1,否则返回0. 裁判测试程序样例: #include <stdio.h> #include <math.h> int IsTheNumber ( const int N ); int ma

PTA 10-排序4 统计工龄 (20分)

题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每个工龄段有多少员工. 输入格式: 输入首先给出正整数NN(\le 10^5≤10?5??),即员工总人数:随后给出NN个整数,即每个员工的工龄,范围在[0, 50]. 输出格式: 按工龄的递增顺序输出每个工龄的员工个数,格式为:"工龄:人数".每项占一行.如果人数为0则不输出该项. 输入样

PTA 邻接表存储图的广度优先遍历(20 分)

6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ); 其中LGraph是邻接表存储的图,定义如下: /* 邻接点的定义 */ typedef struct AdjVNode *PtrToAdjVNode; struct AdjVNode{ Vertex AdjV; /* 邻接点下标 */ PtrToAdjVNode Next; /*

6-1 单链表逆转(20 分)

6-1 单链表逆转(20 分) 本题要求实现一个函数,将给定的单链表逆转. 函数接口定义: List Reverse( List L ); 其中List结构定义如下: typedef struct Node *PtrToNode; struct Node { ElementType Data; /* 存储结点数据 */ PtrToNode Next; /* 指向下一个结点的指针 */ }; typedef PtrToNode List; /* 定义单链表类型 */ L是给定单链表,函数Rever

6-2 顺序表操作集(20 分)

6-2 顺序表操作集(20 分) 本题要求实现顺序表的操作集. 函数接口定义: List MakeEmpty(); Position Find( List L, ElementType X ); bool Insert( List L, ElementType X, Position P ); bool Delete( List L, Position P ); 其中List结构定义如下: typedef int Position; typedef struct LNode *List; str