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 ...
随机推荐
- prop(name|properties|key,value|fn)
prop(name|properties|key,value|fn) 概述 获取在匹配的元素集中的第一个元素的属性值.直线电机选型 随着一些内置属性的DOM元素或window对象,如果试图将删除该属性 ...
- MessagePack Java Jackson Dataformat - POJO 的序列化和反序列化
在本测试代码中,我们定义了一个 POJO 类,名字为 MessageData,你可以访问下面的链接找到有关这个类的定义. https://github.com/cwiki-us-demo/serial ...
- Spring Boot教程(三十九)使用MyBatis注解配置详解(2)
增删改查 MyBatis针对不同的数据库操作分别提供了不同的注解来进行配置,在之前的示例中演示了@Insert,下面针对User表做一组最基本的增删改查作为示例: public interface U ...
- Centos7 内核升级及删除无用内核
导入key rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 安装elrepo的yum源 rpm -Uvh http://www.e ...
- 对象中 new this
构造函数 new后面调用函数,我们称为构造函数(首字母大写) Object() 我们把他视为一个构造函数,构造函数的本质就是一个函数,只不过构造函数的目的是为了创建新对象,为新对象进行初始化(设置对象 ...
- Git客户端执行命令报错: fatal: Authentication failed for'xxxxx.git',但是又不弹出窗口重新输入用户名和密码的解决办法
1.Git版本:Git-2.17.0 2.引起git报错的原因 在变更远程仓库路径的的时候,弹出过一个窗口输入用户名和密码,但是输错了,之后执行任何拉取和更新的命令都会报如下的错: fatal: Au ...
- mysql 更新存在就累加,不存在就插入语法
INSERT INTO tb_http_tomcat_monitor_1 (id,total_res_time,total_req_count,req_dispose_count,queue_size ...
- 前端性能之Chrome的Waterfall
浏览器根据HTML中外连资源出现的顺序,依次放入队列(队列),然后根据优先级确定向服务器获取资源的顺序.同优先级的资源根据HTML中出现的先后顺序来向服务器获取资源. 瀑布中各项内容的含义: 排队: ...
- [redis]redis五种数据类型和应用场景
一.String(字符串)字符串类型是redis最基础的数据结构,首先键是字符串类型,而且其他几种结构都是在字符串类型基础上构建的,所以字符串类型能为其他四种数据结构的学习尊定基础.字符串类型实际上可 ...
- 3、kubeadm初始化Kubernetes集群
同步时间 # ntpdate time.nist.gov k8s集群组成 k8s部署环境 https://kubernetes.io/docs/setup/independent/create-cl ...