news 2026/5/13 15:17:10

【PAT甲级真题】- Hello World for U(20)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【PAT甲级真题】- Hello World for U(20)

题目来源

Hello World for U - PTA
Hello World for U - 牛客

Description

Given any string ofN ( ≥ 5 ) N (≥5)N(5)characters, you are asked to form the characters into the shape ofU. 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 withn 1 n_1n1characters, then left to right along the bottom line withn 2 n_2n2characters, and finally bottom-up along the vertical line withn 3 n_3n3characters. And more, we would likeUto be as squared as possible – that is, it must be satisfied thatn 1 ​ = n 3 = m a x { k ∣ k ≤ n 2 ​ for all 3 ≤ n 2 ≤ N } with n 1 + n 2 + n 3 − 2 = N n_1​=n_3=max \{ k | k≤n_2 \text{​ for all } 3≤n_2≤N \} \text{ with } n_1+n_2+n_3−2=Nn1=n3=max{kkn2​ for all3n2N}withn1+n2+n32=N.

Input Specification:

Each input file contains one test case. Each case contains one string with no less than5 55and no more than80 8080characters in a line. The string contains no white space.

Output Specification:

For each test case, print the input string in the shape ofUas specified in the description.

Sample Input:

helloworld!

Sample Output:

h ! e d l l lowor

题目大意

给出一个字符串,要求按照U形输出该字符串
U形的定义是:对于长为N NN的字符串,一共有n 1 = n 3 n_1=n_3n1=n3行,n 2 n_2n2列,满足n 1 + n 2 + n 3 − 2 = N n_1+n_2+n_3-2=Nn1+n2+n32=N,且n 1 = n 3 ≤ n 2 n_1=n_3\leq n_2n1=n3n2
输出顺序是左侧从上到下,然后底部从左到右,最后右侧从下到上

思路简介

看懂题目的公式后其实很简单
先计算n 2 n_2n2,不难发现,当N + 2 N+2N+23 33的倍数时,n 2 = ( N + 2 ) / 3 n_2=(N+2)/3n2=(N+2)/3
如果在此基础上,长度再多1 11,由于n 1 = n 3 ≤ n 2 n_1=n_3\leq n_2n1=n3n2,多出来的长度只能加到n 2 n_2n2
长度多2 22同理
综上:n 2 = ( N + 2 ) / 3 + ( N + 2 ) % 3 n_2=(N+2)/3+(N+2)\%3n2=(N+2)/3+(N+2)%3

输出时,按照行输出,维护一个左指针l和一个右指针r
n 1 − 1 n_1-1n11行,只输出开头和结尾,每次输入完后l--,r++,其他地方输出空格
最后一行从l输出到r即可

遇到的问题

  1. 无,一遍过

代码

/** * Hello World for U(20) * https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7?problemSetProblemId=994805462535356416 * 模拟 */#include<bits/stdc++.h>usingnamespacestd;voidsolve(){string s;cin>>s;intlen=s.size();intn2=(len+2)/3+(len+2)%3;intn1=(len+2-n2)/2;intl=0,r=len-1;for(inti=0;i<n1-1;++i){for(intj=0;j<n2;++j){if(j==0)cout<<s[l++];elseif(j==n2-1)cout<<s[r--]<<'\n';elsecout<<' ';}}for(inti=l;i<=r;++i){cout<<s[i];}cout<<'\n';}intmain(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//fstream in("in.txt",ios::in);cin.rdbuf(in.rdbuf());intT=1;//cin>>T;while(T--){solve();}return0;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/13 15:09:39

玩转OurBMC第二十六期:OpenBMC固件远程更新原理与实践(下)

栏目介绍&#xff1a;“玩转OurBMC” 是OurBMC社区开创的知识分享类栏目&#xff0c;主要聚焦于社区和BMC全栈技术相关基础知识的分享&#xff0c;全方位涵盖了从理论原理到实践操作的知识传递。OurBMC社区将通过 “玩转OurBMC” 栏目&#xff0c;帮助开发者们深入了解到社区文…

作者头像 李华
网站建设 2026/5/13 15:09:10

2026年AI推理周期开启:CPU逆袭、存储紧缺,半导体投资主线明确

CPU“配角”逆袭之路过去两年&#xff0c;全球资本聚焦AI训练赛道&#xff0c;GPU成核心&#xff0c;英伟达登顶市值巅峰。传统架构中&#xff0c;8颗GPU配1颗CPU&#xff0c;CPU处边缘。2026年&#xff0c;推理落地取代训练成核心&#xff0c;CPU凭冯・诺依曼架构优势重回舞台…

作者头像 李华
网站建设 2026/5/13 15:07:08

地理探测器实战:用Q值量化‘地形’对‘河流’的控制力到底有多强?

地理探测器实战&#xff1a;用Q值量化地形对河流的控制力 河流网络的空间分布从来不是随机形成的&#xff0c;而是地形、气候、地质等多重因素共同作用的结果。作为一名长期从事流域地貌研究的科研人员&#xff0c;我经常需要回答一个核心问题&#xff1a;在我们研究的区域内&a…

作者头像 李华
网站建设 2026/5/13 15:02:06

IDEA 更新 26.1.1 ,没有关于 AI 的更新内容

https://blog.jetbrains.com/idea/2026/04/intellij-idea-2026-1-1/ IDEA 更新 26.1.1 IntelliJ IDEA 2026.1.1 has arrived with several valuable fixes. You can update to this version from inside the IDE, using the Toolbox App, or using snaps if you are a Ubunt…

作者头像 李华