前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT (Advanced Level) Practice 1031 Hello World for U (20分)

PAT (Advanced Level) Practice 1031 Hello World for U (20分)

作者头像
glm233
发布2020-09-28 11:02:50
2850
发布2020-09-28 11:02:50
举报
文章被收录于专栏:glm的全栈学习之路

1031 Hello World for U (20分)

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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
helloworld!

Sample Output:

代码语言:javascript
复制
h   !
e   d
l   l
lowor

好吧,来一个甲级中唯一要求输出字符串的20分题吧

观察输出图形,两竖一横,竖的字符串长度相同,如何确定横和竖的长度大小?

设n1、n2分别代表竖的长度,n1=n2,n3代表底层横的长度,按照题意n1+n2+n3=n+2,其中n为字符串长度

n3是三个数中最大的,且输出图形要尽可能看起来是正方形,什么意思?意思就是这三个数绝对值不超过1啊

一顿分析之后发现n1=n2=(n+2)/3,n3=n+2-n1-n2

之后就是很简单的去推一下哪个位置该输出字符串的哪个字符就好了~

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
    cin>>s;
    int k=s.size()+2;
    int n1=k/3,n2=k/3,n3=k-n1-n2;
    for(int i=1;i<n1;i++)
    {
        for(int j=1;j<=n3;j++)
        {
            if(j==1)cout<<s[i-1];
            else if(j==n3) cout<<s[k-2-i];
            else cout<<" ";
        }cout<<endl;
    }
    for(int i=n1-1;i<k-n2-1;i++)cout<<s[i];
    cout<<endl;
    //while(1)getchar();
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/12/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档