当前位置:网站首页>1031 Hello World for U
1031 Hello World for U
2022-06-29 07:45:00 【Brosto_ Cloud】
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.
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
lowordeduction :
n1 + n2 + n3 = N + 2;
n1 = n3;
Middle space = n2 - 2;
2*n1 + n2 = N + 2;
n1 = n3 = max { k | k ≤ n2 for all 3 ≤ n2 ≤ N } namely n1、n3<=n2;
n2 = N - 2*n1 + 2 >= n1;
N + 2 >= 3*n1;
n1 <= (N+2)/3;
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int N = s.size();
int n1 = (N + 2) / 3;
int n2 = N + 2 - 2 * n1;
for (int i = 0; i <= n1 - 2; i++) {
cout << s[i];
for (int j = 1; j <= n2 - 2; j++) {
cout << ' ';
}
cout << s[N - i - 1];
cout << endl;
}
for (int i = n1 - 1; i <= n1 - 1 + n2 - 1; i++) {
cout << s[i];
}
return 0;
}边栏推荐
猜你喜欢
![[popular science materials] materials from scientific spirit to scientific knowledge](/img/fc/647805056c8ff2b30e894a6e6c4a36.jpg)
[popular science materials] materials from scientific spirit to scientific knowledge

Wechat applet learning notes (summer vacation)

Dump (cl\alv\tree\base================================cp|set\items\for\column) when expanding node or clicking toolbar button

Detailed explanation of top and free commands

并发幂等性防抖

What you should know about databases

Alternative writing of if else in a project

ShapeShifter: Robust Physical Adversarial Attack on Faster R-CNN Object Detector

SQL 注入绕过(六)

excel高级绘图技巧100讲(六)-甘特图在项目进度上的实战应用案例
随机推荐
Loop nesting: why can large loops inside and small loops outside improve the running efficiency of programs
并发幂等性防抖
TF.Slim的repeat和stack操作
Oracle 批量插入数据-插入民族数据
SAP ui5 Beginner (I) Introduction
查看tensorflow是否支持GPU,以及测试程序
matlab 多普勒效应产生振动信号和处理
National Security Agency and CISA kubernetes reinforcement guidelines - new content in version 1.1
【工控老马】基于PLC的花样喷泉设计原理详解
Kyushu cloud helps Inner Mongolia's "counting from the east to the west" project to drive the smart new ecology of the surveying and mapping industry
498. 对角线遍历(模拟)
Markdown skill tree (5): picture
Select distinct on statement in kingbasees
Appium自动化测试基础 — ADB常用命令(三)
Appium自动化测试基础 — ADB常用命令(二)
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
ShapeShifter: Robust Physical Adversarial Attack on Faster R-CNN Object Detector
Kingbasees v8r6 cluster maintenance case - data migration between clusters
1032 Sharing
[translation] how Bink drives the digital loyalty transactions of some of the largest banks in the UK