PAT1031:Hello World for U
1031. Hello World for U (20)
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
思路
对称U型格式输出一个字符串。
穷举找出满足 n1 + n3 + n2 = N + 2(3 <= n2 <= N , n3 = n1 <= n2) 的n1,n3最大值即可。
代码
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main()
{
string s;
while(cin >> s)
{
int N = s.size();
int maxn3 = -;
for(int n2 = ;n2 <= N;n2++)
{
for(int n3 = ;n3 <=n2;n3++)
{
if(*n3 + n2 - == N)
maxn3 = max(maxn3,n3);
}
} int n1 = maxn3,n2 = N + - * n1; //print
int i = ;
for(i = ;i < n1 - ;i++)
{
cout << s[i];
for(int j = ; j < n2 - ;j++)
cout << " ";
cout << s[N - i - ] << endl;
}
int rest = N - i - ;
for(;i <= rest;i++)
{
cout << s[i];
}
cout << endl;
}
}
PAT1031:Hello World for U的更多相关文章
- pat1031. Hello World for U (20)
1031. Hello World for U (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Giv ...
- PAT1031
一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...
随机推荐
- 【Qt编程】Qt 小时钟
Hello World! 学习编程语言的最简单最经典的小程序,当然Qt也不例外.在学习画图时,我觉得写个时钟小程序也是个比较好的开始.在之前的<Matlab及Java小时>一文中,我也从写 ...
- SpriteBuilder中音频文件格式的需要注意的地方
就像在SpriteBuilder项目子目录中的其他资源文件一样,音频文件夹需要确定完整的文件夹路径. 并且如果音频文件输出格式为MP4,则扩展为.m4a(audio-only MPEG4)而不是.mp ...
- eclipse 设置maven来自动下载源码与doc
通常我们通过maven来使用各种库文件,想要真正了解别人的类实现方法,需要查看别人的源码,maven给我们提供了这个便利,它不仅可以下载各种库文件,还会下载对应的源码和doc文档. 一.在工具栏找到W ...
- Unix - 文件中构成一个空洞的分析
lseek函数显示地为一个打开文件设置偏移量,文件偏移量可以大于文件的当前长度,在这种情况下,对该文件的下一次写将加长该文件,并在文件中构成一个空洞,这一点是允许的.位于文件中但没有写过的字节都被读为 ...
- DiskLruCache硬盘缓存技术详解
上次讲了使用内存缓存LruCache去加载很多图片而不造成OOM,而这种缓存的特点是在应用程序运行时管理内存中的资源(图片)的存储和释放,如果LruCache中有一张图片被释放了,再次加载该图片时需要 ...
- SharePoint 2013 图文开发系列之入门教程
做了SharePoint有三年了,大家经常会问到,你的SharePoint是怎么学的,想想自己的水平,也不过是初级开发罢了.因为,SharePoint开发需要接触的东西太多了,Windows操作系统. ...
- 手把手教你从头开始搭建友善之臂ARM-tiny4412开发环境(史上最详细!!)
创建一个ARM目录 mkdir /disk/A9 -p 接下来你需要准备以下的东西 1.arm-linux-gcc-4.5.1 交叉编译器 2.linux-3.5-tiny4412 ...
- 关于STM32的延时问题
最近一直在搞一辆智能小车,用STM32单片机驱动,往上面加了很多外设,外型如下: 今天下午打算在LCD显示一个温度,却发现怎么都显示不了,也找不出原因,还好我们公司的郑工帮我看出了问题,让我顺利改过来 ...
- PyCharm导入pymysql包运行报错问题解决:No module named 'PyMySQL'
import pymysql # 导入包 # 报错问题显示: ImportError: No module named 'PyMySQL' 出现该问题提示:找不到该包名. 解决办法如下: ①先下载Py ...
- 转:<mvc:annotation-driven/>的注解意义
<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案.<mvc:annotation-dri ...