1031. Hello World for U (20) PAT
题目: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的更多相关文章
- 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) ...
- 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 ...
- 1054. 求平均值 (20)-PAT乙级真题
今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...
- PAT甲题题解-1031. Hello World for U (20)-字符串处理,水
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- PAT (Advanced Level) 1031. Hello World for U (20)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- 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 ...
- 【PAT甲级】1031 Hello World for U (20 分)
题意: 输入一个字符串长度为5~80,以'U'型输出,使得底端一行字符数量不小于侧面一列,左右两列长度相等. trick: 不把输出的数组全部赋值为空格为全部答案错误,可能不赋值数组里值为0,赋值后是 ...
- 1027. Colors in Mars (20) PAT
题目:http://pat.zju.edu.cn/contests/pat-a-practise/1027 简单题,考察十进制数和n进制数的转换和输出格式的控制. People in Mars rep ...
- 1031. Hello World for U (20)
题目链接:http://www.patest.cn/contests/pat-a-practise/1031 题目: 分析: 排版题.注意先计算好最后一排的字符数,然后计算前面几排的空格数.难度不大 ...
随机推荐
- PHP面向对象之旅:模板模式(转)
抽象类的应用就是典型的模版模式 抽象类的应用就是典型的模版模式,先声明一个不能被实例化的模版,在子类中去依照模版实现具体的应用. 我们写这样一个应用: 银行计算利息,都是利率乘以本金和存款时间,但各种 ...
- URAL 1062 - Triathlon(半平面交)
这个题乍眼一看好像很简单,然后我就认为u.v.w只要有全部比另外一个人小的就不能win,否则就能win,但是这个思路只对了一半 不能win的结论是正确的,但是win的结论不止排除这一个条件 将这个人与 ...
- Java基础知识强化之集合框架笔记45:Set集合之TreeSet存储自定义对象并遍历练习1(自然排序:Comparable)
1. 自然排序: TreeSet会调用集合元素的compareTo(Object obj)方法来比较元素之间的大小关系,然后将集合元素按照升序排列,这种方式就是自然排序. Java中提供了一个Comp ...
- 关于a标签的链接的表现形式
target属性:self在自身标签页里打开 blank在新标签页中打开 1. 链接到其它地址位置.html文档等 <a href="1.html"></a&g ...
- 处理ASP.NET 请求(IIS)(2)
ASP.NET与IIS是紧密联系的,由于IIS6.0与IIS7.0的工作方式的不同,导致ASP.NET的工作原理也发生了相应的变化. IIS6(IIS7的经典模式)与IIS7的集成模式的不同 IIS6 ...
- shell跑一个PHP脚本的简单命令
最近在做一个刷数据库的小功能,需要批量添加到不同的表中,写好PHP文件之后,登录到某一个服务器上面 上传文件的命令:rz 会出现一个弹框可以选择要上传的文件 执行文件并报错误的命令:/usr/loca ...
- win2008 64位下.net 无法访问oracle
这两天换了台新机子,就想弄个新系统win2008 64bit来测试下,也尝尝新鲜,结果是碰的头破血流啊,哈哈就像挖宝似的 环境:win2008 64bit + IIS7+.net2.0 +ORACLE ...
- angularjs google map markers+ ui-gmap-windows --->增加click 事件
jsp: <div class="modal-body viewOnMap"> <div class="cboxClose" ng-click ...
- 那些常用的eclipse快捷键
用了很久的eclipse了,有些快捷键常用,有的偶尔使用,现在记下常用的快捷键,以便大家和自己查用(持续更新) 这些快捷键都可以在[window]-[preferences]-[general]-[k ...
- ios 键盘弹起
#pragma mark 键盘弹起操作 - (void)keyboardWillShow:(NSNotification *)notification{ NSDictionary *info = ...