题目描述:

Given any string of N (>=) 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 <= n2 <= N } with n1 + n2 + n3 - = N. 输入: There are multiple test cases.Each case contains one string with no less than and no more than characters in a line. The string contains no white space. 输出: For each test case, print the input string in the shape of U as specified in the description. 样例输入: helloworld!
ac.jobdu.com 样例输出: h !
e d
l l
lowor
a m
c o
. c
jobdu.

题目本身不难,在做的过程中遇到了三个问题

1.由于第一次尝试用c++写,虽然跟c语言相差无几,但是有需要注意的细节。用到了string类,需要引入cstring包,但是VC引入string包才能编译通过,在OJ上只能是ctring才能编译通过

2.由于n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N },也就是说n1,n2,n3三个数可以相等,所以在判断的时候要加n1<=n2

3.又由于n2>=3的,最开始做的时候没看到等号,直接让n2从4开始的,导致当n=5的时候出现了错误

修正三个错误后的代码如下:(其中n1,n2分别表示了题目中的n1,n3;x表示题目中的n2)

#include <iostream>
#include <cstring>
#inculde <cstdio> using namespace std;
int main(){
char arr[];
int x;
int i,j;
int n1,n2;
while(scanf("%s",arr)!=EOF){
int n = strlen(arr);
for(x=;x<n;x++){
if((n+-x)%==&&(n+-x)/<=x)
break;
}
n1=n2=(n+-x)/;
for(i=;i<n1-;i++)
{
cout<<arr[i];
for(j=;j<x-;j++)
cout<<" ";
cout<<arr[n-i-];
cout<<"\n";
}
for(i=;i<x;i++)
{
cout<<arr[n1-+i];
}
cout<<"\n";
}
return ;
}


 

随机推荐

  1. 省略号 对单行 多行的css

    .twoline{ display: -webkit-box !important;; overflow:hidden; text-overflow: ellipsis; word-break: br ...

  2. 限制EditText只能输入小数点后两位

    设置EditText只能输入小数点后两位,在价格等有限制的输入时特别有效 TextWatcher textWatcher = new TextWatcher() { @Override public ...

  3. VS2012编译VS2010版本的过程报错解决

    C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32C:\Program Files\MSBuild\Microsoft C:\Pro ...

  4. 使用 HTML5 input 类型提升移动端输入体验

    在过去的几年里,在移动设备上浏览网页已变得难以置信的受欢迎. 但是这些设备上的浏览体验,有时遗留很多的有待改进.当涉及到填写表单时,这一点尤为明显.幸运的是,HTML5规范引入了许多新input类型, ...

  5. __weak

    需要使用弱引用的 三种情况: 1. 如果这个block不被持有,那么你完全没有必要使用__weak 2. 如果被持有了,那么__weak是必然的 3. 如果在多线程并发的情况下,不仅要使用__weak ...

  6. Unit01-OOP-对象和类(上)

    Unit01-OOP-对象和类(上) 1.什么是类?什么是对象?  1)现实生活是由很多很多对象组成的    基于对象抽出了类  2)对象:真实存在的单个的个体    类:类型.类别,代表一类个体  ...

  7. 制作U盘启动盘及安装操作系统的方法

    U盘启动盘制作方法: 1.从网上下载最新的老毛桃U盘启动制作工具主程序并安装 2.插入U盘(制作启动盘前先保存好你的资料到其它地方,以防丢失不可找回) 3.插入正确的U盘后程序会自动检测到U盘,启动模 ...

  8. Inside Flask - json 处理

    Inside Flask - json 处理 在处理 web api 时,json 是非常好用的数据交换格式,它结构简单,基本上各种主流的编程语言都有良好的支持工具. flask 中处理 json 时 ...

  9. shopnc导入商品到大商创

    <?php //select member_name user_name,member_mobile mobile_phone,member_email email,member_passwd ...

  10. JAVA邮件发送的简单实现

    JAVA MAIL是利用现有的邮件账户发送邮件的工具,比如说,我在网易注册一个邮箱账户,通过JAVA Mail的操控,我可以不亲自登录网易邮箱,让程序自动的使用网易邮箱发送邮件.这一机制被广泛的用在注 ...