题目描述:

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. js滚动加载插件

    function $xhyload(o){ var that=this; if(!o){ return; }else{ that.win=$(o.config.obj); that.qpanel=$( ...

  2. 小吐槽Toolbar

    最近弄界面 要吧全部图标改成PNG格式 虽说从2010以后Delphi默认支持PNG格式图片, 但是想应用到按钮上, 似乎除了TButton意外, 也只能ToolBar可以正常显示了, 其他的, 比如 ...

  3. Airline Hub

    参考:http://blog.csdn.net/mobius_strip/article/details/12731459 #include <stdio.h> #include < ...

  4. 让ecshop用户名、手机号、email登陆方法

    让ecshop用户名.手机号.email登陆方法, 仅适用于没有做过任何平台整合的ECSHOP网站   修改文件:   1.includes/modules/integrates/ecshop.php ...

  5. php命名、注释规范

    一.注释 1.文件头部模板 /** *这是一个什么文件 * *此文件程序用来做什么的(详细说明,可选.). * @author richard<e421083458@163.com> * ...

  6. 【转】SQL Server中关于跟踪(Trace)那点事

    前言 一提到跟踪俩字,很多人想到警匪片中的场景,同样在我们的SQL Server数据库中“跟踪”也是无处不在的,如果我们利用好了跟踪技巧,就可以针对某些特定的场景做定向分析,找出充足的证据来破案. 简 ...

  7. Struts(七):action配置文件之通配符映射

    通配符映射:一个Web应用可能有成百上千个action声明,可以使用struts提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系. 通配符映射规则: 若找到多个匹配,没有通配符的那个将胜 ...

  8. 我的新发现:AVL树旋转的一个特性

    关于AVL树旋转的代码网络上铺天盖地. 一些经典的实现方法如下: AVLTree SingleLeftRotation(AVLTree A) { AVLTree B = A->left; A-& ...

  9. Eclipse中使用自己封装的jar包的过程

    在包名上右键,选择Export"" 经过上面的步骤,成功导出了可运行jar包,下面在另一个自己的工程里引入这个jar包 最终效果如下:可见包已经可以正常使用了!! 运行自己的jar ...

  10. java mybatis 中sql 模糊查询

    示例: like concat('%',#{groupName},'%') //-------------- <select id="findList" resultType ...