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

题目描述:

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.

译:给你一个 N (≥5) 个字符的字符串,要求你将这些字符组成 U 的形状。例如, helloworld 可以被打印出来:

h  d
e l
l r
lowo

也就是说,字符必须按照原来的顺序答应,从左垂线开始从上往下有 n1 行字符,最后一行从左至右有 n2 个字符,最后从下往上垂线上有 n3 个字符。更多的是,我们希望 U 尽可能的正方,也就是说它必须满足要求 n1=n3=max { k | k≤n2 for all 3≤n2≤N } 并且 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.

译:每个输入文件包含一个测试用例,每个用例包含一行不少于 5 个字符不超过 80 个字符的字符串。这个字符串不包含空格。


Output Specification (输出说明):

For each test case, print the input string in the shape of U as specified in the description.

译:对于每个测试用例,将输入的字符串按描述的 U 形状输出。


Sample Input (样例输入):

helloworld!

Sample Output (样例输出):

h   !
e d
l l
lowor

The Idea:

首先利用 string 存储字符串,根据 n1=n3=max { k | k≤n2 for all 3≤n2≤N } 并且 n1+n2+n3−2=N. 很容易可以推出:n1 = n3 = (N + 2) / 3 ; n2 = N + 2 - 2 * n1 ; 也就是得到了 U 形状的行与列。

然后按照要求打印即可。


The Codes:

#include<bits/stdc++.h>
using namespace std;
int main(){
string s ;
cin >> s ;
int n1 = (s.size() + 2) / 3 ; // 得到 U 形状有多少行
int n2 = s.size() - 2 * n1 + 2 ; // 得到 U 形状有多少列
for(int i = 0 ; i < n1 - 1 ;){ // 除最后一行以外
printf("%c" , s[i ++]) ; // 输出每行最左边的字符
for(int k = 1 ; k < n2 - 1 ; k ++) printf(" ") ; // 输出中间的空格
printf("%c\n" , s[s.size() - i]) ; // 输出每行最右边的字符
}
cout<< s.substr(n1 - 1 , n2) << endl ; // 输出最后一行
return 0;
}

PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642的更多相关文章

  1. PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...

  2. PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...

  3. PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...

  4. PAT (Advanced Level) Practice 1001 A+B Format (20 分)

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...

  5. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  6. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  7. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  8. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  9. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

随机推荐

  1. ThoughtWorks Homework

    ThoughtWorks Homework Homework 考察知识点 项目搭建 技术选型 测试 编码风格 代码质量 设计模式 数据结构 算法 架构 开源协作 CI/CD DevOps Linux ...

  2. Flutter for Desktop

    Flutter for Desktop https://flutter.dev/desktop https://github.com/flutter/flutter/wiki/Desktop-shel ...

  3. Java如何保证文件落盘?

    本文转载自Java如何保证文件落盘? 导语 在之前的文章Linux/UNIX编程如何保证文件落盘中,我们聊了从应用到操作系统,我们要如何保证文件落盘,来确保掉电等故障不会导致数据丢失.JDK也封装了对 ...

  4. Spark在处理数据的时候,会将数据都加载到内存再做处理吗?

    对于Spark的初学者,往往会有一个疑问:Spark(如SparkRDD.SparkSQL)在处理数据的时候,会将数据都加载到内存再做处理吗? 很显然,答案是否定的! 对该问题产生疑问的根源还是对Sp ...

  5. ES6 声明变量的六种方法

    ES5 只有两种声明变量的方法: var 命令和 function 命令. ES6 除了添加 let 和 const 命令, 后面章节还会提到, 另外两种声明变量的方法: import 命令和 cla ...

  6. JDBC 连接Oracle数据库 各个对象的理解

    JDBC: 1. **代码实现:(连接oracle数据库) ​    1.导入驱动jar包 ​    2.注册驱动 ​     Class.forName("oracle.jdbc.driv ...

  7. React高级

    1.React应用 1.1创建应用 创建项目可以使用react脚手架,创建步骤如下 1)安装react脚手架 npm i -g create-react-app 2)创建项目 create-react ...

  8. 永远不要眼高手低,Vue完整实现一套简单的增删改查CURD操作

    1: 永远不要眼高手低,看起来很简单,但是你从来没有去动手试一下,就不知道其中真正需要注意的许多细节, 2:完整code如下: 1 <!DOCTYPE html> 2 <html l ...

  9. Go的函数

    目录 Go的函数 一.函数的定义 1.函数的基本格式 2.函数的参数 2.1 函数传参的特点:copy传值 3.函数的返回值 4.可变长参数 二.函数的类型 1.给函数的类型重命名 三.匿名函数 1. ...

  10. selectors版socket

    一.作业需求: 使用SELECT或SELECTORS模块实现并发简单版FTP 允许多用户并发上传下载文件 二.readme 一.作业需求: 使用SELECT或SELECTORS模块实现并发简单版FTP ...