首页 > 其他分享 >PAT Advanced 1031 Hello World for U(20)

PAT Advanced 1031 Hello World for U(20)

时间:2022-09-06 21:13:19浏览次数:70  
标签:20 string characters World n1 n2 n3 line 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 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

算法描述:模拟

题目大意:

用所给字符串按U型输出。n1和n3是左右两条竖线从上到下的字符个数,n2是底部横线从左到右的字符个数。
要求:
① n1 == n3
② n2 >= n1
③ n1为在满足上述条件的情况下的最大值

#include<iostream>
using namespace std;

int main()
{
    string str;
    cin >> str;
    int n = str.size();
    int n1 = (n + 2)/ 3 , n3 = n1, n2 = n + 2 - n1 - n3;
    for(int i = 0 ; i < n1 - 1; i++)
    {
        cout << str[i];
        for(int j = 0 ; j < n2 - 2; j++)
        {
            cout << " ";
        }
        cout << str[n - i - 1] << endl;
    }
    for(int i = 0 ; i < n2 ; i++)
        cout << str[n1 + i - 1];
    return 0;
}

标签:20,string,characters,World,n1,n2,n3,line,PAT
From: https://www.cnblogs.com/yztozju/p/16663305.html

相关文章

  • PAT Advanced 1032 Sharing(25)
    题目描述:TostoreEnglishwords,onemethodistouselinkedlistsandstoreawordletterbyletter.Tosavesomespace,wemayletthewordssharethesames......
  • 2022-9-6 #25 None
    这是否也算一种闲话。受到音游的影响,最近一个月都没咋做题,也没更博。我的评价是......
  • 2022/09/26小测 题解
    (本文将笔者测试时的想法及赛后想法均写出来了)题目:话说某某在\(cj\)校运会上异军突起,其实不是偶然,而是有历史原因的。时光回溯到\(XX\)年前,某某为了心中的理想,每天爬......
  • 2022.9.3 随笔
    1.  <script> </script>最好放在</body>的前面,以免出现解释script代码时部分标签还未加载;script代码如果报错,不影响其他代码的解析;2. 一个网页中可以有多对s......
  • Sub-1GHz 315M/433M 无线收发一体芯片-PAN3020
    PAN3020是一款工作在1GHz以下的多个频段,如315MHz/433MHz/868MHz/915MHz(后称为315频段、433频段、868频段和915频段)通用ISM频段的单片无线收发芯片。PAN......
  • "蔚来杯"2022牛客暑期多校训练营9
    A CarShow题意:给定一个数组,请找到有多个区间[L,R]满足1到m的数都出现过。分析:直接双指针就好#include<bits/stdc++.h>usingnamespacestd;longlongn,m,s[......
  • 2022 年 360反馈问题都要问什么?
     您是否尝试过与墙壁或无生命的物体交谈?你表达了你的想法并提出了问题,但由于你没有得到任何回报,我们可以说沟通已经发生了吗?人们常说,只有在有反馈的情况下,正确的沟通才......
  • CCPC2020网络预选赛(vp)
    比赛链接:https://vjudge.net/contest/513012C-ExpressMailTaking题意:有\(n\)个箱子,分别在\(a_1,a_2,...,a_n\)的位置,钥匙在\(k\)的位置,每去打开一个箱子......
  • 洛谷P2002 消息扩散(tarjan缩点)
    题目链接:https://www.luogu.com.cn/problem/P2002思路:由于图中每个强连通分量(scc)中的点是可以互相到达的,所以我们可以用tarjan求图中scc,然后将所有scc缩点,最后求缩点之......
  • 2022最新iOS最新打包发布流程
     关于如何发布iOS应用到AppStroe,苹果开发者中心已经给出了很详细的说明。和普通的iOS应用一样,使用ReactNative开发的iOS应用也需要使用普通的iOS应用的发布流程,总的来......