当前位置:网站首页>1031 Hello World for U
1031 Hello World for U
2022-06-29 06:39: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
lowor推导:
n1 + n2 + n3 = N + 2;
n1 = n3;
中间空格 = n2 - 2;
2*n1 + n2 = N + 2;
n1 = n3 = max { k | k ≤ n2 for all 3 ≤ n2 ≤ N }即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;
}边栏推荐
- uva10891
- Unexpected exception ... code: Badrequest when downloading Xilinx 2018.2
- cv::Mat与Base64转换(含图片压缩解压等流程)
- tf.compat.v1.global_variables
- Markdown skill tree (8): code blocks
- 机器学习笔记 - 时间序列使用机器学习进行预测
- How to select CRM brand suppliers in garment industry?
- 感知健康生活 赋能无界连接 ——为OpenHarmony 3.1生态构建贡献芯海力量
- WebRTC系列-网络传输之8-连通性检测
- mmclassification安装与调试
猜你喜欢

Markdown skill tree (5): picture

Kingbasees v8r6 cluster maintenance case -- single instance data migration to cluster case
什么是测试架构师
![[qnx hypervisor 2.2 user manual]6.2.1 communication between guests](/img/3d/6a0cde206a09a7ef03038fb99cf532.png)
[qnx hypervisor 2.2 user manual]6.2.1 communication between guests

【科普资料】从科学精神到科学知识的材料

tf.count_nonzero

Spark RDD case: Statistics of daily new users

【FreeRTOS】中断机制

IMX6DL4.1.15支持EIM总线(上)——实际操作,修改内容。

软件测试面试如何正确谈论薪资?
随机推荐
excel高级绘图技巧100讲(六)-甘特图在项目进度上的实战应用案例
等保备案主体是谁?在当地网安进行备案是吗?
Markdown 技能树(4):链接
Comment choisir les fournisseurs de marques CRM dans l'industrie du vêtement?
关联性——相关性分析
Schnuka: 3D machine vision inspection system 3D vision inspection application industry
The table cannot be vacuumed because the cursor is open for a long time
uva10635
数字ic设计——UART
感知健康生活 赋能无界连接 ——为OpenHarmony 3.1生态构建贡献芯海力量
利用Jsonp跨域请求数据
Imx6dl4.1.15 supports EIM bus (Part 2) - configuration principle analysis.
蓝桥杯——13届第二批试题解析
mmclassification安装与调试
循环嵌套问题:为什么大循环在内,小循环在外可以提高程序的运行效率
施努卡:3d视觉检测方案 3d视觉检测应用行业
Webrtc series - 8-connectivity detection for network transmission
tf.compat.v1.assign
[translation] E-Cloud. Large scale CDN using kubeedge
什么是测试架构师