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 ...
随机推荐
- spark实现smote近邻采样
一.smote相关理论 (1). SMOTE是一种对普通过采样(oversampling)的一个改良.普通的过采样会使得训练集中有很多重复的样本. SMOTE的全称是Synthetic Minorit ...
- exgcd 解同余方程ax=b(%n)
ax=n(%b) -> ax+by=n 方程有解当且仅当 gcd(a,b) | n ( n是gcd(a,b)的倍数 ) exgcd解得 a*x0+b*y0=gcd(a,b) 记k=n/gc ...
- [题解] [AHOI2009] 跳棋
题面 题解 分类讨论, 考虑到只要所有的偶数点上都有棋子, 最左边的棋子就可以跳到最右边 题目第一问让我们求最少的在白格子上必须放的棋子数(不用考虑行动中放的棋子数) 考虑到这几种情况 有不少于两个红 ...
- ARTS打卡计划第五周
Algorithms: https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 采用了map的 ...
- 【Nginx】Linux 环境下 Nginx 配置SSL 证书
一.解压三个包到相同目录编译nginx cd /usr/local/src/nginx-1.12.2 # 将下列的包版本号换成自己下载的版本号 ./configure --prefix=/usr/lo ...
- 精讲JS逻辑运算符&&、||,位运算符|,&
1.JS中的||符号: 运算方法: 只要“||”前面为false,不管“||”后面是true还是false,都返回“||”后面的值. 只要“||”前面为true,不管“||”后面是true还是fals ...
- java获取来访者mac信息
根据IP获取对应的Mac地址,支持win10+Linux package com.simonjia.util.other; /** * @Author: SimonHu * @Date: 2019/6 ...
- 移动平台对meta标签的定义
一.meta 标签分两大部分:HTTP 标题信息(http-equiv)和页面描述信息(name). 1.http-equiv 属性的 Content-Type 值(显示字符集的设定) 说明:设定页面 ...
- Angular5.0之 安装指定版本Angular CLI
我们可能会发现按照网上的方式下载安装后,使用Angular CLI生成的项目并不是我们想要的Angular的版本,因为在我们没有指定安装版本的前提下,默认会下载最新的版本安装,然而不同的Angular ...
- nginx环境准备
一.环境调试确认 1.四项确认 确认公网已连通. 确认yum源可用. 确认iptables已经关闭. 确认selinux已经关闭. a.确认是否连通公网. ping www.baidu.com b.确 ...