立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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


题意:输入一个字符串,按给定规则输出。

规则:

1、按样例中顺序输出字母
2、n1+n2+n3 = N+2
3、n2 >= 3
4、n1 <= n2

低级解法:定义双重循环,i循环为n2,从3开始遍历;j循环为n1,从i开始遍历,等于0时停止 。 找到解后,按给定规律输出。(解放双手,暴力万岁)

高级解法:手动推导,可得结论:n1=n3=(N+2)/3向下取整。n1,n2,n3确定以后按规定输出。


低级解法

#include<bits/stdc++.h>
using namespace std;
int main() {ios::sync_with_stdio(false);string s; cin >> s;int len = s.length(), b, l_r;for(int i = 3; ; i++)                     //求n1和n2 for(int j = i; j >= 1; j--) if(j*2 + i == len+2) {b = i; l_r = j-1; goto loop;}loop:;//输出 int k = 0;   //计数器 for(int i = 0; i < l_r+1; i++) {   //行数 for(int j = 0; j < b; j++) { //列数 if(i != l_r) if(j == 0) putchar(s[k]);else if(j == b-1) putchar(s[len- k++ -1]);  else putchar(' ');else putchar(s[k++]); } putchar('\n'); }return 0;
}

高级解法

#include<bits/stdc++.h>
using namespace std;
int main(){string input;  cin>> input//读取字符串int n1=(input.size()+2)/3,n2=input.size()+2-2*n1;//获取n1,n2//进行输出for(int i=0;i<n1-1;++i){//前n1-1行,需要输出首尾两个字符,且每行字符数为n2printf("%c",input[i]);for(int j=0;j<n2-2;++j)printf(" ");printf("%c\n",input[input.size()-i-1]);}//最后一行将没有输出的n2个字符一次性输出for(int i=0;i<n2;++i)printf("%c",input[n1-1+i]);return 0;
}

耗时:

【题意+推导讲解】1031 Hello World for U (20 分)_15行代码AC相关推荐

  1. 【题意+解析】1041 Be Unique (20 分)_18行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Being unique is so important to people on Mars that even their lo ...

  2. 【题意+分析】1071 Speech Patterns (25 分)_27行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 People often have a preference among synonyms of the same word. F ...

  3. 【题意+分析】1067 Sort with Swap(0, i) (25 分)_24行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any permutation of the numbers {0, 1, 2,-, N−1}, it is easy ...

  4. 【题意分析】1024 Palindromic Number (25 分)_38行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A number that will be the same when it is written forwards or bac ...

  5. 15行代码AC——Link/Cut Tree CodeForces - 614A(爆long long处理+快速幂讲解)

    励志用少的代码做高效表达 Problem describe Programmer Rostislav got seriously interested in the Link/Cut Tree dat ...

  6. 38行代码AC——UVA-167The Sultan‘s Successors(八皇后问题,附视频讲解)

    最近备考蓝桥,学习到递归模块,从最基本的八皇后及其变种开始刷起(如果可以穿越,我一定要抓到发明递归的那个人,然后把他干掉,造福后世的算法er,). 题目大意 一个人,没孩子,要在死前分割财产,然后出了 ...

  7. 43行代码AC——HDU 1757 A Simple Math Problem(矩阵快速幂,附快速幂讲解)

    一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 代码(去掉空行43行) #include<iostream> #inclu ...

  8. 1031 Hello World for U (20 分)【难度: 一般 / 知识点: 找规律】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805462535356416 #include<bits/stdc+ ...

  9. 【PAT甲级 U形打印】1031 Hello World for U (20 分) Java版 6/6通过

    题目 一开始没看懂"as squared as possible"和那个max不等式到底是什么意思,看了别人的解答才明白: 如果n % 3 == 0,n正好被3整除,直接n1 = ...

最新文章

  1. 纪念小柴昌俊 | 中微子天体物理学的诞生
  2. 实录分享 | 计算未来轻沙龙:计算机视觉与图形学(PPT下载)
  3. 宏观经济学自我学习与总结
  4. How is component metadata dependency dependencies consumed in the runtime
  5. jquery检测浏览器类型
  6. spring cloud分布式整合zipkin的链路跟踪
  7. 30岁学python有前途吗-30岁新手入门python!尝试人生另一种可能
  8. 18家机构批量刷新SOTA!T5 is all you need!
  9. ES6学习笔记对象的扩展(补充)
  10. paip.ASP 开发调试大总结
  11. BootStrap-datepicker日期插件
  12. Linux 创建.sh脚本文件
  13. C语言素数ns流程图,请各位大神帮个忙,画个NS流程图,,急!!!
  14. css中标准盒模型和怪异盒模型的区别,如何将标准盒模型转换为怪异盒模型
  15. STM32CubeMX安装(全图文安装步骤,一步不落下)
  16. [DAX] MIN函数 | MINX函数
  17. 计算机和网络连接不上,电脑宽带连不上怎么办_台式电脑连不上宽带怎么回事-win7之家...
  18. java输出GPA_请完成下列Java程序:实现换算GPA,对于学生学习的每一门课程,都输入两个..._考试资料网...
  19. java后端面试总结
  20. Ubuntu 22.04 LTS root登录、修改当前用户名和主机名

热门文章

  1. 顶级极客技术挑战赛,你敢来挑战吗?| 大神登峰造极
  2. 万字整理,图解Linux内存管理所有知识点
  3. Go gomaxprocs 调高引起调度性能损耗
  4. 浅析低延迟直播协议设计:RTP/RTCP
  5. 基于webrtc多人音视频的研究(一)
  6. unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass
  7. golang 数组 切片 下标范围
  8. 说一下对象或数组转JSON怎么转【fastjson】
  9. Vue012_ 自定义插件
  10. Spark _09资源调度和任务调度