PAT 甲级 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 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 3 } with n1+n2+n3−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
题意:
一开始没看懂题意,看懂以后一遍过。
尽可能围成正方形。在满足n1=n3,且n1<=n2的条件下,n1尽可能地大。
底部字符数量 n2 最少为3。n1 = n3 要尽量等于 n2 。也就是说,n1 = n3 = (n+2) / 3;因为 '/ 3' 会截掉小数部分,所以 n1 和 n3 总是小于或者等于 n2 。
AC代码:
#include<bits/stdc++.h>
using namespace std;
char a[];
int main(){
cin>>a;
int l=strlen(a);
int n1=(l+)/;
int n2=l-n1*;
//cout<<n1<<" "<<n2<<endl;
for(int i=;i<n1-;i++){
cout<<a[i];
for(int j=;j<=n2;j++){
cout<<" ";
}
cout<<a[l--i]<<endl;
}
for(int i=n1-;i<=n1+n2;i++){
cout<<a[i];
}
return ;
}
PAT 甲级 1031 Hello World for U (20 分)(一开始没看懂题意)的更多相关文章
- PAT甲级:1136 A Delayed Palindrome (20分)
PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...
- PAT 甲级 1054 The Dominant Color (20 分)
1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...
- PAT 甲级 1027 Colors in Mars (20 分)
1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way a ...
- PAT 甲级 1005 Spell It Right (20 分)
1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...
- PAT 甲级 1058 A+B in Hogwarts (20 分) (简单题)
1058 A+B in Hogwarts (20 分) If you are a fan of Harry Potter, you would know the world of magic ha ...
- PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- 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甲级】1054 The Dominant Color (20 分)
题意: 输入两个正整数M和N(M<=800,N<=600),分别代表一张图片的宽度和高度,接着输入N行每行包括M个点的颜色编号,输出这张图片主导色的编号.(一张图片的主导色占据了一半以上的 ...
- 【PAT甲级】1001 A+B Format (20 分)
题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6≤a,b≤1e6) AAAAAccepted code: #include<bits/stdc++.h> us ...
随机推荐
- java中使用redis --- Hash的简单应用
1.java代码 public class RedisTest01 { public static void main(String[] args) { // connect redis server ...
- 2.3 vue配置(上)
rm,在打包之前把上一次打包之后的东西删掉,然后webpack重新打包 通过DefinePlugin形成一个环境变量 HTML打包插件
- 基于JS的高级脚本语言 Sara
Sara-基于JS的高级脚本语言 欢迎使用Sara,Sara是一款基于JavaScript的全新的高级脚本语言! Sara不像我们工作室上一款编程语言作品-Ginit一样,他属于更高级的语言 Sara ...
- 55、servlet3.0-ServletContext注册三大组件
55.servlet3.0-ServletContext注册三大组件 使用ServletContext 注册 Servlet.Filter.Listener 使用编码的方式,在项目启动的时候给 Ser ...
- SQLServer常见查询问题
http://bbs.csdn.net/topics/340078327 1.生成若干行记录 --自然数表1-1M CREATE TABLE Nums(n int NOT NULL PRIMAR ...
- Jquery tabs
官网 http://api.jqueryui.com/tabs/ 必须通过了后台验证tab1的信息后才允许进入tab2 var passed=false; $("#tabs ...
- luogu 5505 [JSOI2011]分特产 广义容斥
共有 $m$ 种物品,每个物品 $a[i]$ 个,分给 $n$ 个人,每个人至少要拿到一件,求方案数. 令 $f[i]$ 表示钦定 $i$ 个没分到特产,其余 $(n-i)$ 个人随便选的方案数,$g ...
- SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread
解决方法: 在 接口方法中添加 事务注解 即可. public interface IBase<PK extends Serializable, T> { @Transactional v ...
- Jedis API操作redis数据库
1.配置文件 classpath路径下,新建redis.properties配置文件 配置文件内容 # Redis settings redis.host=127.0.0.1 redis.port=6 ...
- 树莓派4硬件---GPIO篇
树莓派拿到手已经两个多月了,其实从最开始的期待安装好ROS,到前几天完成了ROS的源码编译安装,对linux的调教也时花了些时间的.现在终于想起来,树莓派上还有GPIO,还没有用过了.说干就干,开始. ...