PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642
PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642
题目描述:
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.
译:给你一个 N (≥5) 个字符的字符串,要求你将这些字符组成 U 的形状。例如, helloworld 可以被打印出来:
h d
e l
l r
lowo
也就是说,字符必须按照原来的顺序答应,从左垂线开始从上往下有 n1 行字符,最后一行从左至右有 n2 个字符,最后从下往上垂线上有 n3 个字符。更多的是,我们希望 U 尽可能的正方,也就是说它必须满足要求 n1=n3=max { k | k≤n2 for all 3≤n2≤N } 并且 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.
译:每个输入文件包含一个测试用例,每个用例包含一行不少于 5 个字符不超过 80 个字符的字符串。这个字符串不包含空格。
Output Specification (输出说明):
For each test case, print the input string in the shape of U as specified in the description.
译:对于每个测试用例,将输入的字符串按描述的 U 形状输出。
Sample Input (样例输入):
helloworld!
Sample Output (样例输出):
h !
e d
l l
lowor
The Idea:
首先利用 string 存储字符串,根据 n1=n3=max { k | k≤n2 for all 3≤n2≤N } 并且 n1+n2+n3−2=N. 很容易可以推出:n1 = n3 = (N + 2) / 3 ; n2 = N + 2 - 2 * n1 ; 也就是得到了 U 形状的行与列。
然后按照要求打印即可。
The Codes:
#include<bits/stdc++.h>
using namespace std;
int main(){
string s ;
cin >> s ;
int n1 = (s.size() + 2) / 3 ; // 得到 U 形状有多少行
int n2 = s.size() - 2 * n1 + 2 ; // 得到 U 形状有多少列
for(int i = 0 ; i < n1 - 1 ;){ // 除最后一行以外
printf("%c" , s[i ++]) ; // 输出每行最左边的字符
for(int k = 1 ; k < n2 - 1 ; k ++) printf(" ") ; // 输出中间的空格
printf("%c\n" , s[s.size() - i]) ; // 输出每行最右边的字符
}
cout<< s.substr(n1 - 1 , n2) << endl ; // 输出最后一行
return 0;
}
PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642的更多相关文章
- PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642
PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...
- PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...
- PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...
- PAT (Advanced Level) Practice 1001 A+B Format (20 分)
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642
PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...
随机推荐
- Vue 组件之间通信 All in One
Vue 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 https://stackblitz.com/edit/vue-parent-child-commutation?fil ...
- Python数据结构与算法_最长公共前缀(05)
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- 自己写的一个抢票加速的Python小程序源码分享-----纯属娱乐
最近这段时间频频看到微信群里发什么 抢票加速,智行.携程.飞猪.美团,对于我这能坐客车就不坐火车的人来说,无所谓靠谱不靠谱 突发奇想的整理了下整个抢票加速的逻辑,写了这个小程序,代码很low,拒绝批评 ...
- jQuery编写组件的几种方式
原文链接:https://w.cnblogs.com/xiao-xi/p/8572471.html 三种方式: 1.通过$.extend()来扩展jQuery 2.通过$.fn 向jQuery添加新的 ...
- DQL:data query language用来查询数据库表中的数据
对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 如果没有查询条件,则每次查询所有的行.实际应用中,一般要指定查询的条件.对记录进行过滤. 查询 ...
- Spring Boot和Thymeleaf整合,结合JPA实现分页效果
在项目里,我需要做一个Spring Boot结合Thymeleaf前端模版,结合JPA实现分页的演示效果.做的时候发现有些问题,也查了现有网上的不少文档,发现能全栈实现的不多,所以这里我就把我的做法, ...
- go的循环
目录 go的循环 一.语法 二.语法简写 1.省略第一部分 2.省略第二部分 3.省略第三部分 4.全省略:死循环 5.终极写法,简洁变形 go的循环 Go中只有for循环,没有while循环.因为w ...
- 【图像处理】使用OpenCV进行图像处理教程(一)
OpenCV是进行图像处理的工具,也是计算机视觉领域近十几年不断发展和完善的产物.面对这个已基本成熟的开源库知识体系,我们新生代有必要不断地总结.回顾,以新的视角快速融入计算机视觉的奥秘世界. 从这篇 ...
- 实现 Abp Vnext Pro
Abp Vnext Pro 的 Vue 实现版本 开箱即用的中后台前端/设计解决方案 知识点 .Net Core5.0 Abp Vnext 4.x , Ant Design, Vue2.x Mysql ...
- 利用Visual Studio调试JavaScript脚本
方法1: 方法2: 打开IE,按F12调试. 方法3: JS断电点debugger代替