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, ...
随机推荐
- CoordinatorLayout
CoordinatorLayout作为"super-powered FrameLayout"基本实现两个功能: 1.作为顶层布局 2.调度协调子布局 CoordinatorLa ...
- butternife Zelezny自动注入插件
插件地址:http://plugins.jetbrains.com/plugin/7369 Products: IntelliJ IDEA, RubyMine, WebStorm, PhpStorm, ...
- 关于webp图片格式初探
前言 不管是 PC 还是移动端,图片一直是流量大头,以苹果公司 Retina 产品为代表的高 PPI 屏对图片的质量提出了更高的要求,如何保证在图片的精细度不降低的前提下缩小图片体积,成为了一个有价值 ...
- 解决Cell重用内容混乱的几种简单方法,有些方法会增加内存
重用实现分析 查看UITableView头文件,会找到NSMutableArray* visiableCells,和NSMutableDictnery* reusableTableCells两个结构 ...
- 云技术:负载均衡SLB
什么是SLB? SLB是Server Load Balance(负载均衡)的简称,XX云计算有限公司提供的负载均衡服务,通过设置虚拟服务IP,将位于同一机房的多台云服务器资源虚拟成一个高性能.高可用的 ...
- linux下让irb实现代码自动补全的功能
我不知道其他系统上irb是否有此功能,但是在ubuntu上ruby2.1.2自带的irb默认是没有代码自动补全功能的,这多少让人觉得有所不便.其实加上也很简单,就是在irb里加载一个模块:requir ...
- 图文并茂的生产者消费者应用实例demo
前面的几篇文章<<.NET 中的阻塞队列BlockingCollection的正确打开方式>><<项目开发中应用如何并发处理的一二事>>从代码以及理论角 ...
- CSS基础:替换元素和非替换元素
简介 根据 "外在盒子" 是内联还是块级我们可以把元素分为内联元素和块级元素,而根据是否具有可替换内容,我们也可以把元素分为替换元素和非替换元素.这种通过修改某个属性值,例如 &l ...
- CSS 文章链接
文本溢出显示为省略号 Ellipsis for text overflow in table cell?
- Oracle——多表查询
本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查询一起完成各个复杂查询的操作: 一 ...