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
分析
由题可得1:2*n1+n2=N+2;
2:n1<=n2;
3:3<=n2<=N;
由1,3可得 n2>=(N+2)/3,再由1计算出n1
所以 n2 等于最小的大于等于(N+2)/3的整数,但是如果计算出n1,n2不是整数的话(即 N+2-n2 不是偶数的话),就把n2+1,在算出n1;
至于输出格式的控制,可以先输出n1-1行(先输出s[i],在输出n2-2个空格,再输出s[s.size()-1-i]),然后输出剩余的底部就行了。
#include<iostream>
#include<math.h>
using namespace std;
int main(){
string s;
cin>>s;
int n1,n2,n3;
n2=ceil((s.size()+2)/3.0);
n2=(s.size()+2-n2)%2>0?n2+1:n2;
n1=(s.size()+2-n2)/2;
for(int i=0;i<n1-1;i++){
cout<<s[i];
for(int j=0;j<n2-2;j++)
cout<<" ";
cout<<s[s.size()-i-1]<<endl;
}
for(int i=n1-1;i<n1-1+n2;i++)
cout<<s[i];
return 0;
}
1031. Hello World for U的更多相关文章
- BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6014 Solved: 2503[Submit ...
- Light OJ 1031 - Easy Game(区间dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1031 题目大意:两个选手,轮流可以从数组的任意一端取值, 每次可以去任意个但仅 ...
- MySQLdb 1031 Error
Python import MySQLdb 有可能报:site-packages/pkg_resources.py:1031: UserWarning: /home/***/.python-eggs ...
- 深度优先搜索 codevs 1031 质数环
codevs 1031 质数环 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一个大小为N(N<=17)的质数环是 ...
- loj 1031(区间dp+记忆化搜索)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...
- PAT乙级 1031. 查验身份证(15) 标志要清零!!!!!!!!!
1031. 查验身份证(15) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 一个合法的身份证号码由17位地区. ...
- 【BZOJ】【1031】【JSOI2007】字符加密Cipher
后缀数组 当年感觉好神的题现在好像变水了…… 题意其实有点蛋疼……一开始没看懂<_< 将原串复制一遍接在后面,用后缀数组求一下SA,那么SA<n的就是所找到的那n个字符串,然后把它们 ...
- PAT-乙级-1031. 查验身份证(15)
1031. 查验身份证(15) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 一个合法的身份证号码由17位地区. ...
- HDU 1031 Design T-Shirt
http://acm.hdu.edu.cn/showproblem.php?pid=1031 题意 :n个人,每个人对m件衣服打分,每个人对第 i 件衣服的打分要加起来,选取和前 k 高的输出他们的编 ...
- bzoj 1031: [JSOI2007]字符加密Cipher 後綴數組模板題
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3157 Solved: 1233[Submit ...
随机推荐
- bzoj2115 [Wc2011] Xor——高斯消元 & 异或线性基
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2115 异或两次同一段路径的权值,就相当于没有走这段路径: 由此可以得到启发,对于不同的走法, ...
- WinRAR 激活码(KEY)
引自:http://blog.sina.com.cn/s/blog_6fd3b4800100nyk0.html WinRAR 是一个强大的压缩文件管理工具.它能备份你的数据,减少你的 E-mail 附 ...
- java io流读取 和commons.io的使用
前提:记事本里面一共有605个字 1.使用BufferedReader和FileReader来读取txt里面的内容,用时相对短.读完记得关闭流br.close() 2.指定UTF-8输出格式,使用Fi ...
- python/shell脚本报异常^M: bad interpreter: No such file or directory
问题:在Windows写了一python脚本,上传Linux服务器执行,报异常*****^M: bad interpreter: No such file or directory 原因:window ...
- Linux环境下安装JDK并配置环境变量
首先查看是否已经安装了JDK并配置环境变量. [root@dhcc_plat opt]# java -version -bash: java: command not found [root@dhcc ...
- rman 问题
1. RMAN Repeatedly Fail To Backup Archivelogs with RMAN-20242 Cause: There is a mis-match between th ...
- C. Coin Troubles 有依赖的背包 + 完全背包变形
http://codeforces.com/problemset/problem/283/C 一开始的时候,看着样例不懂,为什么5 * a1 + a3不行呢?也是17啊 原来是,题目要求硬币数目a3 ...
- C#模拟百度登录并到指定网站评论回帖(五)
前面的四篇已经说完了全部的模拟百度登录,接下来就是到指定的网站去发表评论,当然你也可能是获取其他信息,其实这些套路都是万变不离其宗的,弄懂之后觉得像这种请求-响应获取信息的功能实在很简单(想起当初走的 ...
- Android中出现Error:In (declare-styleable) FontFamilyFont, unable to find attribute android:font
Android中出现Error:In (declare-styleable) FontFamilyFont, unable to find attribute android:font 解决办法,今天 ...
- Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载
元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...