题目:
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. python s12 day3

    python s12 day3   深浅拷贝 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  2. Node.js + Express + Mongodb 开发搭建个人网站(一)

    一.Node + Express环境搭建 0.去Node官网下载安装node,如果安装了 npm 和 node的话 那么就 安装 全局的 express,-g全局安装 npm install expr ...

  3. HTML5 WebAudioAPI(四)--绘制频谱图2

    绘制分析器数组所有数据.本文内容,承接上文 1.800宽度绘制 var url='../content/audio/海阔天空.mp3'; if (!window.AudioContext) { ale ...

  4. Difference Between XML and XAML.

    XML, or Extensible Markup Language, is a subset  of the more complex SGML (Standard Generalized Mark ...

  5. XFire中Services.xml 配置的一些细节

    昨天第一次调XFire引擎,放在Tomcat里之后老是报错,检查了很久没有发现什么问题,一直很费解. 后来在网上看到一篇文章<XFire services.xml 配置文件补充说明>,根绝 ...

  6. Express在windows IIS上部署详解

    最近公司在用Express+angularjs+wcf开发系统,让我在windows上部署系统,遇到不少问题,不过最后还是解决了,在IIS上部署系统, 首先windows需安装以下软件: 1.node ...

  7. oracle数据库读取操作系统的物理文件-转载,待完善

    --源地址不详 --创建目录SQL> create directory dir_xls as '/home/oracle'; Directory created. --给用户授权SQL> ...

  8. AppStore上架规则

    1. 条款和条件1.1 为App Store开发程序,开发者必须遵守 Program License Agreement (PLA).人机交互指南(HIG)以及开发者和苹果签订的任何协议和合同.以下规 ...

  9. C# RSA加密/解密

    RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的公钥加密算法, ...

  10. SGU 163.Wise King

    一道题目长的水题.... 总结就一句话,给出n个(-3~3)的数,一个数m,取任意个数是使这些数的m次幂之和最大. code #include <iostream> #include &l ...