题目描述:

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. .Net using,string.Empty初探

    前两天够哦年公司培训,讲了编码优化.现在初步总结下:(有些不大确定的就不讲了) 多次字符串拼接(特别是循环内),宜用stringBuilder.Append()方法,少用字符串+,至于string.F ...

  2. yii框架分页

  3. Fiddler-010-网络延时应用小技巧-模拟低网速环境

    在日常的网络测试中,经常需要测试网络超时或在网络传输速率不佳的情况的应用场景,而与此同时我们有时手边资源有限,实现在各种真实网络(2G\3G)环境下测试有些局限性.其实 fiddler 已经提供了类似 ...

  4. c#创建、安装、卸载、调试windows服务的简单事例

    最近工作中用到了windows服务,对其有深刻理解和丰富经验谈不上,本篇文章只是简单陈诉用c#创建.安装.卸载.调试windows服务的步骤. 一.创建windows服务 1.用VS创建windows ...

  5. Win8.1密钥

    Win8.1 在线永久激活密钥一枚!  78BHN-M3KRH-PCP9W-HQJYR-Q9KHD [剩余次数:7K多+] 继续增加 [Key]:HPCJW-VGYW4-CR7W2-JG6Q7-K4Q ...

  6. Python开发【第九章】:线程、进程和协程

    一.线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务 1.t ...

  7. iOS小技巧3

    将颜色合成图片 将颜色合成图片 +(UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1 ...

  8. RDIFramework.NET ━ 9.16 案例模块━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.15  案例模块 -Web部分 9.16.1.产品管理模块 产品管理模块提供了基本的增.删.改.查.导出.分页等的实现,用户可 ...

  9. 注解的基本盘点 -- 《Java编程思想》

    注解(元数据)为我们在代码中添加信息提供了一种形式化的方法,使我们可以在之后的某一个时刻非常方便地使用这些数据. ---<Java编程思想> 其实注解可以理解为一个工具类,只要使用了这个工 ...

  10. Ruby(rails)win环境下安装

    1.RubyInstaller 在RubyInstaller官网下载window版本安装,地址:http://rubyinstaller.org/downloads/  执行安装程序,勾选Add Ru ...