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 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

解题思路

在本题中,需要根据题目的需要分析题目并且得到每一列和每一行所需要输出的字符的个数。在上述的要求中,要求k≤n​2并且n​1​​+n​2​​+n​3​​−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相关推荐

  1. PAT甲级(Advanced Level)真题--1046 Sharing

    PAT甲级(Advanced Level)真题–1046 Sharing 通过:648 提交:1138 通过率:56% To store English words, one method is to ...

  2. 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 ...

  3. 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 ...

  4. Pat甲级 1002 A+B for Polynomials

    Pat甲级 1002 A+B for Polynomials 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems ...

  5. Pat甲级 1001 A+B Format

    Pat甲级 1001 A+B Format 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems/99480552 ...

  6. PAT甲级1055 The World‘s Richest:[C++题解]k路归并

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 采用二维数组vector[N]来存每个年龄的人(结构体),然后分别从大到小排序.剩下的任务就是从给定的年龄[a ,b]中,k路归并最 ...

  7. PAT甲级1051 Pop Sequence:[C++题解]模拟栈、判断序列是否是合法的出栈序列

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 题意:将1~N压栈,判断给定序列是否是合法的出栈序列. 对于序列1~N中的每个值i,先将其压入栈.然后对于它就有两种处理方法:要么压 ...

  8. PAT甲级1085 Perfect Sequence :[C++题解]双指针

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:求满足条件M≤m×pM \leq m\times pM≤m×p的区间[m, M]最长是多少.此处有一性质:当最大值M变大的时候,最小值 ...

  9. PAT甲级1046 Shortest Distance:[C++题解]前缀和

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 用前缀和快速求出一段的和.注意求两段,取最小值. ac代码 #include<bits/stdc++.h> using ...

  10. PAT甲级1029 Median:[C++题解]贪心、二路归并

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 如果直接排序,时间复杂度是O(nlogn),n=2∗105O(nlogn),\ n=2*10^5O(nlogn), n=2∗105会 ...

最新文章

  1. IOS后台运行机制详解(一)
  2. 为Pdf批量添加水印
  3. JS编程建议——2:正确辨析JavaScript句法中的词、句和段
  4. tensorflow错误记录:tf.concat
  5. 20191001:String,StringBuffer,StringBuilder类异同辨析
  6. springBoot 2.2.6 项目中html页面样式效果丢失
  7. java的dicon文件_配置文件参考
  8. 《Oracle SQL疑难解析》——1.10 删除表中的所有行
  9. Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)
  10. python字典修改属性_Python2.7格式化字典键属性的递归调用
  11. PyCharm中配置模板
  12. windows下支持H265的rtmp ffplay播放器 ffmpeg的编译
  13. Matlab求整数规划
  14. 消费品企业,会员营销四大痛点
  15. 奥斯汀计算机专业排名,德克萨斯大学奥斯汀分校
  16. 房产门户网站系统 有租房 二手房 新房功能 房产网站源码
  17. 二进制中,0为什么作为偶数,1为什么作为奇数?
  18. 自定义控件其实很简单 三
  19. 3dmax基本物体放置操作1
  20. MATH1013总结

热门文章

  1. 亲爱的波特兰——CJ麦科勒姆告别信
  2. java即时通讯 开源_java开源即时通讯软件服务端openfire源码构建
  3. firefox渗透安全插件汇总
  4. 【专利】如何画专利流程图(逻辑图)
  5. N年前的实习记录 - 职场生涯应如何规划?
  6. 网络爬虫Python试验
  7. MyDockFinder(mydock myfinder合二为一版)
  8. 网络直播与营销“合二为一”
  9. 我的第一本社会心理学(part4)--自我偏差
  10. B+树索引实战:全值匹配查询