当前位置:网站首页>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;
}边栏推荐
- Use of LSTM neural network and general neural network
- 循环嵌套问题:为什么大循环在内,小循环在外可以提高程序的运行效率
- Kingbasees coping with transaction rollback caused by too fast growth of table age
- Explain canfd message and format in AUTOSAR arxml in detail
- Some examples.
- Appium 环境搭建
- AI与元宇宙擦出火花:人类失去的只有枷锁,获得的是全方面的解放
- 1032 Sharing
- Dump (cl\alv\tree\base================================cp|set\items\for\column) when expanding node or clicking toolbar button
- Alicloud access resource: nosuchkey
猜你喜欢

帆船动力学仿真分析

Appium automation test foundation ADB common commands (II)

Kingbasees v8r6 cluster maintenance case -- single instance data migration to cluster case
![[industrial control old horse] detailed design of PLC six way responder system](/img/9c/8bfe336bb95a028a4fb8130941ff81.png)
[industrial control old horse] detailed design of PLC six way responder system

Schnuka: 3D machine vision inspection system 3D vision inspection application industry

【FreeRTOS】中断机制

Vibration signal generation and processing based on MATLAB Doppler effect

matlab 多普勒效应产生振动信号和处理

4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
![[industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC](/img/28/690f9985f32675f5d50d196c293abe.jpg)
[industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC
随机推荐
游标长时间open导致表无法vacuum问题
软件测试鸾音鹤信
Compiling principle: the king's way
电检码配置
Appium automation test foundation ADB common commands (II)
施努卡:3d机器视觉检测系统 3d视觉检测应用行业
Selected Siemens PLC project example source code [300 sets in total]
Markdown skill tree (1): introduction to markdown
About the problem that the kingbasees temporary file is too large
358. K 距离间隔重排字符串 排序
Matlab Simulink simulation and analysis of power grid sweep frequency
100 lectures on Excel advanced drawing skills (VI) - practical application cases of Gantt chart in project progress
Concurrent idempotent anti shake
DataTables screen error Popup
Golang modifying the value of a structure slice
【FreeRTOS】中断机制
关于KingbaseES临时文件过大问题
数组知识点小结
Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200
Reflection modification final