题目:
http://pat.zju.edu.cn/contests/pat-a-practise/1031

分析:

简单题,调整数组的输出次序就可以了。

输入数组长度为len。n1 = n3 = (len+2)/3;   n2 = len - n1 - n3;

第一行最左边对应的是原数组的第一个字符,最后边对应的是原数组的最后一个字符。总共输出n1行。

题目描述:

Given any string of N (>=5) 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 n 1 characters, then left to right along the bottom line with n 2 characters, and finally bottom-up along the vertical line with n 3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n 1 = n 3 = max { k| k <= n 2 for all 3 <= n 2 <= N } with n 1 + n 2 + n 3 - 2 = N.

Input Specification:

Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.

Output Specification:

For each test case, print the input string in the shape of U as specified in the description.

Sample Input:

helloworld!

Sample Output:

h   !
e d
l l
lowor

参考代码:

#include<iostream>
#include<string.h>
using namespace std;
#define max 85
char input[max]; int main()
{
int len;
int n1,n2,n3;
int i,j;
cin>>input;
len = strlen(input);
n1 = n3 = (len + 2) / 3 ;
n2 = len - 2*n1;
for(i=0; i<n1-1; i++)
{
cout<<input[i];
for(j=0; j<n2; j++) cout<<" ";
cout<<input[len-1-i]<<endl;
}
for(j=i; j<len-i; j++)
cout<<input[j];
cout<<endl; return 0;
}

1031. Hello World for U (20) PAT的更多相关文章

  1. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...

  2. PAT 甲级 1031 Hello World for U (20 分)(一开始没看懂题意)

    1031 Hello World for U (20 分)   Given any string of N (≥) characters, you are asked to form the char ...

  3. 1054. 求平均值 (20)-PAT乙级真题

    今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...

  4. PAT甲题题解-1031. Hello World for U (20)-字符串处理,水

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  5. PAT (Advanced Level) 1031. Hello World for U (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  6. PAT Advanced 1031 Hello World for U (20 分)

    Given any string of N (≥) characters, you are asked to form the characters into the shape of U. For ...

  7. 【PAT甲级】1031 Hello World for U (20 分)

    题意: 输入一个字符串长度为5~80,以'U'型输出,使得底端一行字符数量不小于侧面一列,左右两列长度相等. trick: 不把输出的数组全部赋值为空格为全部答案错误,可能不赋值数组里值为0,赋值后是 ...

  8. 1027. Colors in Mars (20) PAT

    题目:http://pat.zju.edu.cn/contests/pat-a-practise/1027 简单题,考察十进制数和n进制数的转换和输出格式的控制. People in Mars rep ...

  9. 1031. Hello World for U (20)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1031 题目: 分析: 排版题.注意先计算好最后一排的字符数,然后计算前面几排的空格数.难度不大 ...

随机推荐

  1. 利用 SysRq 键排除和诊断系统故障

    说白了,SysRq手动触发而不用命令, /proc/sysrq-trigger 这个是通过命令接口 实验:LINUX窗口下 ,CTRL+ALT+F1,切换到TTY1,在文本模式下,按下 ALT+Sys ...

  2. 使用vi是方向键变乱码 退格键不能使用的解决方法

    一.编辑/etc/vim/vimrc.tiny 由于/etc/vim/vimrc.tiny的拥有者是root用户,所以要在root的权限下对这个文件进行修改.很简单,这个文件里面的倒数第二句话是“se ...

  3. ios中图片的绘画和截图

    ios中图片的绘画和截图 CGImageCreateWithImageInRect截图和UIGraphicsGetImageFromCurrentImageContext绘画图片 使用CGImageC ...

  4. Driving the Activity Lifecycle

    Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActiv ...

  5. 函数返回一个SqlDataReader对象

    解决方法: 1.尝试使用dataset 2.执行数据库操作命令 SqlDataReader reader=mySqlCommand.ExecuteReader(CommandBehavior.Clos ...

  6. 在vSphere5.0虚拟机里的Ubuntu Server 32位安装JDK

    本机操作系统Win7 服务器用vSphere 5.0 虚拟机  在虚拟机安装了Ubuntu Server 12.04 1.首先到Oracle官网上下载jdk-7u51-linux-i586.tar.g ...

  7. C# - string 转为 DateTime(自定义)

    上代码: string dt = " 1 11 1961"; DateTime day; System.Globalization.DateTimeFormatInfo dtFor ...

  8. Objective-C中的分类与协议

    分类 在谈分类之前,我们可以先探究下,OC中为什么出现分类这种机制,有什么好处? 假设你接到一个大项目:计算两个整数的和,差.接到任务的你马上动手.编写代码如下: #import <Founda ...

  9. APNs改动 (转)

    对 APNs 的吐槽 APNs 是 Apple Push Notification service 的简称(注意 APNs 的大小写, s不需要大写). 以下是我收集的一些关于 APNs 的吐槽,你先 ...

  10. iOS开发之指定UIView的某几个角(小于4)为圆角

    在iOS开发中,我们经常会遇到View设置圆角的问题,如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore ...