PAT甲级 A1031
PAT甲级 A1031
题目情况
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 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≤n2≤N } 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
解题思路
在本题中,需要根据题目的需要分析题目并且得到每一列和每一行所需要输出的字符的个数。在上述的要求中,要求k≤n2并且n1+n2+n3−2=N,我们联立以上不等式和等式即可得到当前的K的限制:k≤(n+2)/3,按照此思路得到k即可进行输入:
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main() {string a; cin >> a;int n = a.size();int k = (n + 2) / 3;int n2 = n + 2 - k - k;int out = n2 - 2;for (int i = 0; i < k; i++) {cout << a[i];if (i == k - 1) {for (int j = 1; j <= out; j++) {cout << a[i + j];}}else {for (int j = 0; j < out; j++) {cout << " ";}}cout << a[a.size() - 1 - i];cout << '\n';}
}
PAT甲级 A1031相关推荐
- PAT甲级(Advanced Level)真题--1046 Sharing
PAT甲级(Advanced Level)真题–1046 Sharing 通过:648 提交:1138 通过率:56% To store English words, one method is to ...
- PAT甲级(Advanced Level)真题-- 1062 To Buy or Not to Buy
PAT甲级(Advanced Level)真题-- 1062 To Buy or Not to Buy 通过:643 提交:1220 通过率:52% Eva would like to make a ...
- PAT甲级真题 1018 A+B in Hogwarts--python解法
PAT甲级真题 1018 A+B in Hogwarts 提交:2638 通过:1559 通过率:59% If you are a fan of Harry Potter, you would kno ...
- Pat甲级 1002 A+B for Polynomials
Pat甲级 1002 A+B for Polynomials 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems ...
- Pat甲级 1001 A+B Format
Pat甲级 1001 A+B Format 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems/99480552 ...
- PAT甲级1055 The World‘s Richest:[C++题解]k路归并
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 采用二维数组vector[N]来存每个年龄的人(结构体),然后分别从大到小排序.剩下的任务就是从给定的年龄[a ,b]中,k路归并最 ...
- PAT甲级1051 Pop Sequence:[C++题解]模拟栈、判断序列是否是合法的出栈序列
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 题意:将1~N压栈,判断给定序列是否是合法的出栈序列. 对于序列1~N中的每个值i,先将其压入栈.然后对于它就有两种处理方法:要么压 ...
- PAT甲级1085 Perfect Sequence :[C++题解]双指针
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:求满足条件M≤m×pM \leq m\times pM≤m×p的区间[m, M]最长是多少.此处有一性质:当最大值M变大的时候,最小值 ...
- PAT甲级1046 Shortest Distance:[C++题解]前缀和
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 用前缀和快速求出一段的和.注意求两段,取最小值. ac代码 #include<bits/stdc++.h> using ...
- PAT甲级1029 Median:[C++题解]贪心、二路归并
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 如果直接排序,时间复杂度是O(nlogn),n=2∗105O(nlogn),\ n=2*10^5O(nlogn), n=2∗105会 ...
最新文章
- IOS后台运行机制详解(一)
- 为Pdf批量添加水印
- JS编程建议——2:正确辨析JavaScript句法中的词、句和段
- tensorflow错误记录:tf.concat
- 20191001:String,StringBuffer,StringBuilder类异同辨析
- springBoot 2.2.6 项目中html页面样式效果丢失
- java的dicon文件_配置文件参考
- 《Oracle SQL疑难解析》——1.10 删除表中的所有行
- Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)
- python字典修改属性_Python2.7格式化字典键属性的递归调用
- PyCharm中配置模板
- windows下支持H265的rtmp ffplay播放器 ffmpeg的编译
- Matlab求整数规划
- 消费品企业,会员营销四大痛点
- 奥斯汀计算机专业排名,德克萨斯大学奥斯汀分校
- 房产门户网站系统 有租房 二手房 新房功能 房产网站源码
- 二进制中,0为什么作为偶数,1为什么作为奇数?
- 自定义控件其实很简单 三
- 3dmax基本物体放置操作1
- MATH1013总结