当前位置:网站首页>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;
}边栏推荐
- 蓝桥杯——最小框架
- Concurrent idempotent anti shake
- 498. 对角线遍历(模拟)
- 1031 Hello World for U
- From XX import* is equivalent to from XX import *, and no space is required
- Loop nesting: why can large loops inside and small loops outside improve the running efficiency of programs
- 719. 找出第 K 小的数对距离(二分)
- Check whether tensorflow supports GPU and test program
- 施努卡:3d视觉检测方案 3d视觉检测应用行业
- [industrial control old horse] detailed design of PLC six way responder system
猜你喜欢

100 lectures on Excel advanced drawing skills (VI) - practical application cases of Gantt chart in project progress

手把手系列---安装SpotBugs、并快速上手使用

Appium automation test foundation ADB common commands (II)
How to view software testing training? Do you need training?

What you should know about databases
What is a Test Architect
What tools do testers need to know

施努卡:什么是视觉定位系统 视觉定位系统的工作原理

Perceiving healthy life, enabling boundless connection -- contributing to openharmony 3.1 ecological construction

matlab simulink 电网扫频仿真和分析
随机推荐
Markdown skill tree (1): introduction to markdown
Appium environment setup
tf.compat.v1.global_variables
National Security Agency and CISA kubernetes reinforcement guidelines - new content in version 1.1
呕心沥血总结出来的MySQL常见错误以及解决方法(二)
路由详解(九阳真经)
Cross domain data request using jsonp
Unexpected exception ... code: Badrequest when downloading Xilinx 2018.2
阿里云访问资源:NoSuchKey
并发幂等性防抖
呕心沥血总结出来的MySQL常见错误以及解决方法(一)
Kingbasees v8r6 cluster maintenance case -- single instance data migration to cluster case
Markdown skill tree (6): List
Markdown skill tree (3): title
[popular science materials] materials from scientific spirit to scientific knowledge
蓝桥杯——13届第二批试题解析
cv::Mat与Base64转换(含图片压缩解压等流程)
循环嵌套问题:为什么大循环在内,小循环在外可以提高程序的运行效率
Select distinct on statement in kingbasees
Fluent imitates uiswitch