题目描述:

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. hibernate学习(4)——实体配置详解

    1.实体 编写规则 提供一个无参数 public访问控制符的构造器 提供一个标识属性,映射数据表主键字段,hibernate以id识别,必须有主键 所有属性提供public访问控制符的 set  ge ...

  2. Wordpress 标题设置

    使用标题格式:首页(网站标题 - 网站副标题),其他页面(页面标题 | 网站标题) 在后台找到头部文件head.php <?php wp_title('|', true, 'right'); e ...

  3. JavaMail接、收邮件

    我总算把这个研究出来啦.... 不要觉得 代码有点多哈. 我们先来说发送邮箱吧,首先建立一个属性文件*.properties sys.properties server=smtp.163.com ## ...

  4. DOM9大节点

    ELEMENT_NODE 1 元素节点 常用 ATTRIBUTE_NODE 2 属性节点 常用 TEXT_NODE 3 文本节点 常用 CDATA_SECTION_NODE 4 CDATA区段   E ...

  5. http://www.cnblogs.com/ACMer/p/5170255.html

    http://www.cnblogs.com/ACMer/p/5170255.html

  6. sql操作之修改记录值

    mysql修改.删除数据记录 用update修改记录 UPDATE tbl_name SET 要更改的列 WHERE 要更新的记录 这里的 WHERE 子句是可选的,因此如果不指定的话,表中的每个记录 ...

  7. django静态文件数据库设置

    STATIC_URL = '/static/'STATICFILES_DIRS = (        os.path.join(BASE_DIR,'static')) DATABASES = {    ...

  8. tomcat access log 参数

    %a - 客户端IP地址 %A - 本机IP地址 %b - 发送字节数,不含HTTP头 如果为空是  '-' %B - 同上 %h - 客户端机器名 (如果connector的enableLookup ...

  9. 手动启动mongodb和nodejs程序

    最近做单片机去了,以前用的mongodb和nodejs没有配置成服务,居然忘了如何手动启动.在此记录下 一.手动启动mongodb 1.进入cmd 2.在dos下打开mongodb路径E:\mongo ...

  10. NSFileManager 遍历目录

    NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentPath = [NSHomeDirecto ...