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 ...
随机推荐
- foxmail地址簿导入thunderbird的乱码问题 (转载)
转自:http://blog.csdn.net/gexueyuan/article/details/9032595 由于foxmail的地址簿格式和thunderbird的格式不一样,另外也存在编码问 ...
- debian下使用dpkg来安装/卸载deb包 (转载)
转自:http://blog.csdn.net/zhou_2008/article/details/6076900 在debian下,你可以使用dpkg(Debian package system)来 ...
- bzoj 2165: 大楼【Floyd+矩阵乘法+倍增+贪心】
1<<i的结果需要是long long的话i是long long是没用的--要写成1ll<<i--我别是个傻子吧 虽然写的是二进制贪心,但是我觉得二分可能更好写吧(但是会慢) ...
- bzoj1052覆盖问题(二分+贪心)
1052: [HAOI2007]覆盖问题 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2446 Solved: 1131[Submit][Stat ...
- 数据传递-------@RequestParam
package com.wh.handler; /** * @RequestParam是传递参数的. * @RequestParam用于将请求参数区数据映射到功能处理方法的参数上. * * publi ...
- win7任务计划提示”该任务映像已损坏或已篡改“怎么处理
https://jingyan.baidu.com/article/e75057f2038e2febc91a8915.html 在命令行窗口(cmd)执行命令:schtasks /query /v ...
- 构建一个.net的干货类库,以便于快速的开发 - 工具类
相信每一个开发的框架都会有一个工具类,工具类的作用有很多,通常我会将最常用的方法放在工具类里 取得用户IP 取得网站根目录的物理路径 枚举相关 非法关键字检查 绝对路径改为相对路径 获取小数位(四舍五 ...
- IIS网站部署步骤以及常见异常解决方案
一. 简述 如果VS调试代码每次都使用浏览器打开,修改脚本和样式等还可以刷新页面显示最新修改,但是修改后台代码的话就需要停止调试再重新使用浏览器打开才能显示后台的最新修改,就比较麻烦.这里推荐附加到I ...
- IE 浏览器在地址栏输入中文字符,发送get请求报400错误的问题
因为学校有JavaWeb的课程,所以才接触这方面.最近遇到了个小问题. 先看一段很简单的jsp代码例子 <%@ page language="java" import=&qu ...
- 解决:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
今天发布web API,调用接口报错了:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31b ...