Hello World for U
题目描述:
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 ;
} -
随机推荐
- Yii源码阅读笔记(二十八)
Yii/web中的Controller类,实现参数绑定,启动csrf验证功能,重定向页面功能: namespace yii\web; use Yii; use yii\base\InlineActio ...
- ExtJS笔记 Store
The Store class encapsulates a client side cache of Model objects. Stores load data via a Proxy, and ...
- Flink - state
public class StreamTaskState implements Serializable, Closeable { private static final long serial ...
- vmware centos6.5 net 配置
使用NAT模式 虚拟机网络连接使用NAT模式,物理机网络连接使用Vmnet8. 虚拟机设置里面——网络适配器,网络连接选择NAT模式. 虚拟机菜单栏—编辑—虚拟网络编辑器,选择Vmnet8 NAT模式 ...
- 《奥威Power-BI智能分析报表制作方法》精彩回顾
年的最后一个月,一年又快过去.工作和学习都不能耽误,本周三奥威公开课又如约与大家见面咯!不知老师教的图文报表在课后你们都有练习吗?趁热打铁,我们现在再次来温习一下吧. 本期分享的内容:<奥威Po ...
- [CC]Plugin-提取ISS3D关键点
基于CloudCompare开发的提取ISS3D关键点. void qLxPluginPCL::doISS3D() { assert(m_app); if (!m_app) return; const ...
- R&&rstudio
R sudo apt-get install R-base Rstudio()下载Rstudio桌面版失败 sudo apt-get install gdebi-core 如果提示少了什么东西,运行下 ...
- JQ的基本架构
Jquery的基本架构 引入 以前学习原生JS然后切换到用JQ的时候总觉得很不习惯,甚至有点排斥用JQ.后来自己写项目一直到公司实习用JQ的这段时间,才深深感受到JQ的强大~JQ不仅做到兼容很多 ...
- Visual C++ 2008进行MySQL编程
visual c++ 2008进行MySQL编程(ODBC) -- (一) 套装安装 visual c++ 2008进行MySQL编程(ODBC) --(二) CDatabase操作数据库 visua ...
- 看门外汉如何实现:C#操作 MongoDB基本CURD的事务控制之 第二部分
第二部分 尝试解决BulkWrite(List<WriteModel<T>>)问题 在上次发表的文章中,得到了一些很好的反馈,真切体会到写博文的好处,有高人指出两大问题,具体可 ...