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, ...
随机推荐
- SpriteBuilder中如何平均拉伸精灵帧动画的距离
首先要在Timeline中选中所有的精灵帧,可以通过如下2种的任意一种办法达成: 1按下Shift键的同时鼠标单击它们 2鼠标在Timeline空白区拖拽直到拉出的矩形包围住所有精灵帧方块后放开鼠标. ...
- 安卓笔记--Style的继承
比如想要重写一个对话框的style <style name="Theme_dialog" parent="@android:style/Theme.Dialog&q ...
- Android NFC开发(一)——初探NFC,了解当前前沿技术
Android NFC开发(一)--初探NFC,了解当前前沿技术 官方文档:http://developer.android.com/guide/topics/connectivity/nfc/ind ...
- 【LaTeX排版】LaTeX论文排版<一>
本文及接下来的几篇文章主要讲关于毕设论文的排版. 1.论文的整体构架 学校规定论文字数不得少于15000:说明论文属于中篇论文.一般来说,中长篇论文采用book文类,短篇论文采用article ...
- rails中validates_confirmation_of验证方法无效的解决办法
rails的model中提供了很多种自带的验证方法,validates_confirmation_of可以验证变量xxx和xxx_confirmation是否相等:这可以用于验证2遍输入的密码是否一致 ...
- JVM学习--(六)类加载器原理
我们知道我们编写的java代码,会经过编译器编译成字节码文件(class文件),再把字节码文件装载到JVM中,映射到各个内存区域中,我们的程序就可以在内存中运行了.那么字节码文件是怎样装载到JVM中的 ...
- Kubernetes如何支持有状态服务的部署?
作者:Jack47 转载请保留作者和原文出处 PS:如果喜欢我写的文章,欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. Kubernetes对无状态服务有完善的支持 ...
- 春天的事务之9.3编程式事务 - 跟我学spring3
9.3编程式事务 9.3.1编程式事务概述 所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理. Spring框架提供一致的事务抽象,因此对于JDBC还是JTA事务都是采用相同 ...
- JeeSite
JeeSite是一个 开源的企业信息管理系统 基础框架.主要定位于"企业信息管理"领域,可用作企业信息管理类系统.网站后台管理类系统等.JeeSite是非常强调开发的高效性.健壮性 ...
- spring 配置多数据源(mysql读写分离)
前段时间刚换了家新公司,然后看项目代码里用了数据库读写分离的架构,然后好奇扒了代码简单看了下,总体来说就是运用spring aop切面方式来实现的.看明白后就在自己的个人小项目里运用了下,测试OK,所 ...