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 ...
随机推荐
- ngtos 天融信
NGFW系列产品基于天融信公司10年高品质安全产品开发经验结晶的NGTOS系统架构,采用了多项突破性技术.基于分层的设计思想,天融信公司通过长期的安全产品研发经验,分析多种安全硬件平台技术的差异,创造 ...
- 2019-2020-1 20199312《Linux内核原理与分析》第二周作业
c语言代码 // main.c int g(int x) { return x + 4; } int f(int x) { return g(x); } int main(void) { return ...
- 定时器 延时调用setTimeout
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WebService操作
webservice是一种服务器通信技术,封装了socket.
- MySQL 优化--持续整理
一.innodb体系结构优化: 1.IO优化 IO能力不足时 innodb_io_capacity 应该降低 innodb_max_dirty_pages_pct 应该降低 innodb_max_di ...
- jQuery相关方法5----表单相关
一.value属性在表单的相关操作-----val()方法 <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js ...
- qt 在windows 以及android 运用资源时的路径使用用限制
qt中存在以下几种路径的使用方式. 1.qrc内置资源在应用程序中属于只读的资源,作为应用程序的一部分,而不是在一个文件夹中与app分离.资源文件在.qrc文件中的路径如 <file>i ...
- UOJ450 【集训队作业2018】复读机【生成函数】
题目链接:UOJ EI神仙加强版 既然这题模数是今天日期减去\(7\times 10^5\),那就要赶紧把这题做了. 首先肯定是考虑指数型生成函数,列出来之后使用单位根反演一波. \[\begin{a ...
- Mac 下 安装 Nginx
---恢复内容开始--- Mac 下 安装nginx 首先确定自己有安装homebrew 安装 nginx brew install nginx 启动nginx 1.15版本下 安装是 直接在ngin ...
- redis简单消息队列
<?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->flushall(); $redis-& ...