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, ...
随机推荐
- UE4 动画系统
1. 准备一套带动作的模型,并导入UE4 2. 新建一个动画蓝图,右键->Animation->AnimationBlueprint,选择继承AnimationInst ...
- Android Studio下使用NDK的流程
我要重新拿回持之以恒徽章!! 老规矩,先说看能学会什么:ANDROID STUDIO下NDK的使用方法.JNI的基本使用方法,C语言调用JAVA的方法. 首先要下载NDK,如果你没有VPN可以来htt ...
- Android ORM 框架之 greenDAO
前言 我相信,在平时的开发过程中,大家一定会或多或少地接触到 SQLite.然而在使用它时,我们往往需要做许多额外的工作,像编写 SQL 语句与解析查询结果等.所以,适用于 Android 的ORM ...
- 【生活随笔】Introspection of my life in 2014
2014年已过去两星期,有写年度总结的必要了.今天特意看了看去年1月5日写的2013年度总结,看看都有些什么变化.我发现每年作一次总结是很有必要的,无赖恰逢考试周,连元旦都不能好好过,更不用说写 ...
- Linux - mail
使用者邮件信箱: mail 使用 wall, write 毕竟要等到使用者在在线才能够进行,有没有其他方式来联络啊? 不是说每个 Linux 主机上面的用户都具有一个 mailbox 吗? 我们可否寄 ...
- PLSQL表
PL/SQL表 一,什么是PL/SQL表? 首先PL/SQL表和记录(Record)一样,都是复合数据类型.可以看做是一种用户自定义数据类型. PL/SQL表由多列单行的标量构成的临时索引表对象.组成 ...
- asp.net core上使用Redis探索(2)
在<<asp.net core上使用Redis探索(1)>>中,我介绍了一个微软官方实现Microsoft.Extensions.Caching.Redis的类库,这次,我们使 ...
- 简单了解AJAX
// ajax的俩个版本: var xmlhttp; if(window.xmlHttpRequest){ xmlhttp = new xmlHttpRequest(); }else{ xmlhttp ...
- 进程间通信——IPC之共享内存
共享内存是三个IPC机制中的一个.它允许两个不相关的进程访问同一个逻辑内存.共享内存是在两个正在进行的进程之间传递数据的一种非常有效的方式. 大多数的共享内存的实现,都把由不同进程之间共享 ...
- Python_让人脑阔疼的编码问题(转)+(整理)
我们要知道python内部使用的是unicode编码,而外部却要面对千奇百怪的各种编码,比如作为中国程序经常要面对的gbk,gb2312,utf8等,那这些编码是怎么转换成内部的unicode呢? 首 ...