PTA(Advanced Level)1031.Hello World for U
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
思路
- 要使得图像尽可能像正方形,且底部边长要\(\ge\)两边边长,由\(n_1+n_2+n_3-2 =N\)可知\(n_1,n_3\)要较小,极限情况是\(n_1=n_2=n_3\),那么如何保证\(n_1=n_3\le n_2\)呢,让\(n_1=n_3=(n+2)/2\),因为是向下取整的关系,所以一定会有\(n_1=n_3\le n_2\)成立
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin >> s;
int n = s.size();
int vertical, bottom;
vertical = (n + 2) / 3;
bottom = n - 2*vertical + 2;
int l = 0, r = n - 1;
for(int i=0;i<vertical-1;i++)
{
cout << s[l];
for(int j=0;j<bottom-2;j++) cout << " ";
cout << s[r];
cout << endl;
l++; r--;
}
for(int i=l;i<=r;i++)
cout << s[i];
return 0;
}
引用
https://pintia.cn/problem-sets/994805342720868352/problems/994805462535356416
PTA(Advanced Level)1031.Hello World for U的更多相关文章
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PTA (Advanced Level) 1009 Product of Polynomials
1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...
- PTA (Advanced Level) 1008 Elevator
Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- PTA (Advanced Level) 1006 Sign In and Sign Out
Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...
- PTA (Advanced Level) 1005 Spell It Right
Spell It Right Given a non-negative integer N, your task is to compute the sum of all the digits of ...
随机推荐
- 『HGOI 20190917』Lefkaritika 题解 (DP)
题目概述 一个$n \times m$的整点集.其中$q$个点被m被设置为不能访问. 问这个点集中含有多少个不同的正方形,满足不包含任何一个不能访问的点. 对于$50\%$的数据满足$1 \leq n ...
- flask框架(四):通过局域网访问网站
一:开启局域网访问 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000) # 设置成局域网访问 二:设置windows的入站规则 ...
- MySQL_8.0与5.7区别之账户与安全
一.创建用户和用户授权 MySQL5.7创建用户和用户授权命令可以同时执行 grant all privileges on *.* to 'Gary'@'%' identified by 'Gary@ ...
- [BZOJ2208]:[Jsoi2010]连通数(暴力 or bitset or 塔尖?)
题目传送门 题目描述 度量一个有向图连通情况的一个指标是连通数,指图中可达顶点对的个数. 在上图中,顶点1可以到达1.2.3.4.5. 顶点2可以到达2.3.4.5. 顶点3可以到达3.4.5. 顶点 ...
- Word文档怎么从第二页加页码
1.首先将光标放到首页的最后位置 2.“页面布局”—“分隔符”—“下一页” 3.“插入”—“页码”—选一种样式的页码 4.将光标定位到第二页的页脚处,“设计”—取消“链接到前一条页眉” 5.将第二页的 ...
- 离线安装nuget包EPPlus
1先去https://www.nuget.org/packages/EPPlus/4.1.0下载,epplus.4.1.0.nupkg 2找到本地文件位置:H:\DOWNLOAD\ 3在vs的程序包管 ...
- Struts 简单UI标签
<!-- 服务器标签 : 最终别解析为html标签--> <s:form action="/user_login" method="post" ...
- Linux 关机/重启命令
Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definiti ...
- sql输出表中重复数据
数据: 1 1 2 32 2 2 33 1 2 34 2 2 35 2 1 36 1 1 37 3 2 1 表格查询: SELECT * FROM `t1`; 可以看到,如果界定为 a.b.c 都相同 ...
- php安装compoer install
1.先确定php运行版本为7.1以上 2.在phpstorm中 或者在项目根目录按住shift+有单击点击“在此处打开命令窗口”运行composer install 3出现这个证明安装成功 . 会遇到 ...