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 ...
随机推荐
- Prism框架中View与Region关联的几种方式
Prism.Regions命名空间下有2个重要接口:IRegionManager.IRegion IRegionManager接口中的方法与属性:AddToRegion().RegisterViewW ...
- 上传项目到码云或GitHub
一.安装Git 官网下载地址:https://git-scm.com/download/win 安装完成后,配置环境变量即可, 打开cmd,输入 git,出现以下提示即表示安装成功: 二.生成ssh公 ...
- Neko Performs Cat Furrier Transform CodeForces - 1152B 二进制思维题
Neko Performs Cat Furrier TransformCodeForces - 1152B 题目大意:给你一个x,在40步操作以内把x变成2m−1,m为非负整数.对于每步操作,奇数步可 ...
- Liblinear Visual studio 2013 Error C3057
使用LibLinear时编译时出现Error C3057的错误: OpenMP报错,查询MSDN: #pragma omp threadprivate(var)//var在编译之前必须是确定值 所以修 ...
- 使用powershell管理域用户
在域内环境中,常常需要使用命令行管理域用户,此时可以使用Active Directory中的命令行工具Dsquery.exe,或CSVE,以及Ldifde等,其实,使用Windows Powershe ...
- hive分区表插入一条测试数据
1.show create table tb_cdr;+-------------------------------------------------------+--+| ...
- svn 同步备份的所有问题,亲测可用
svnsync 异地同步收获 (2010-07-06 10:06:19) 转载▼ 标签: 杂谈 分类: svn svnsync 异地同步收获: 来自:我用Subversion - http://www ...
- BeanFactory和ApplicationContext的区别+部分Spring的使用
BeanFactory和ApplicationContext的区别 ApplicationContext 方式加载:创建容器的同时 容器初始化,容器所有的bean创建完毕 Spring容器中获取一 ...
- python3笔记目录大纲汇总
篇一.python3基础知识和语句 python3笔记一:python基础知识 python3笔记二:进制转换与原码反码补码 python3笔记三:运算符与表达式 python3笔记四:if语句 py ...
- pycharm2019连接mysql错误:08801 ------Connection to django1@localhost failed. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.