PTA(Advanced Level)1031.Hello World for U
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
思路
- 要使得图像尽可能像正方形,且底部边长要\(\ge\)两边边长,由\(n_1+n_2+n_3-2 =N\)可知\(n_1,n_3\)要较小,极限情况是\(n_1=n_2=n_3\),那么如何保证\(n_1=n_3\le n_2\)呢,让\(n_1=n_3=(n+2)/2\),因为是向下取整的关系,所以一定会有\(n_1=n_3\le n_2\)成立
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin >> s;
int n = s.size();
int vertical, bottom;
vertical = (n + 2) / 3;
bottom = n - 2*vertical + 2;
int l = 0, r = n - 1;
for(int i=0;i<vertical-1;i++)
{
cout << s[l];
for(int j=0;j<bottom-2;j++) cout << " ";
cout << s[r];
cout << endl;
l++; r--;
}
for(int i=l;i<=r;i++)
cout << s[i];
return 0;
}
引用
https://pintia.cn/problem-sets/994805342720868352/problems/994805462535356416
PTA(Advanced Level)1031.Hello World for U的更多相关文章
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PTA (Advanced Level) 1009 Product of Polynomials
1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...
- PTA (Advanced Level) 1008 Elevator
Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- PTA (Advanced Level) 1006 Sign In and Sign Out
Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...
- PTA (Advanced Level) 1005 Spell It Right
Spell It Right Given a non-negative integer N, your task is to compute the sum of all the digits of ...
随机推荐
- 自己封装的Java excel数据读取方法
package org.webdriver.autotest.data; import jxl.Workbook; import jxl.Sheet; import jxl.Cell; import ...
- html5原生js拖拽上传(golang版)
一次只能传一个文件,需在main.go同级目录中建一个upload文件夹 main.go package main import ( "fmt" "io" &q ...
- Xargs用法详解(自创)
简介之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如: 这个命令是错误的find /sbin -perm +700 |ls -l ...
- Robotframework之SSHLibrary库
Robotframework之SSHLibrary库 使用robotframework做自动化测试,在流程中可能需要远程连接机器做一些简单操作,比如连接linux服务器,外面平时用的工具去连接 ...
- Python 图形界面元素
from tkinter import * import os def button_click1(): try: filePath = r'D:\CloudMusic' os.system(&quo ...
- 认识git的简单命令
一.课程目的 教大家学会it的简单命令(老师建议 English 的前后要加空格) 二.认识 git 命令 整篇文章都是用语雀写的. 解释了(使用git)绑定gitee的操作 https://www. ...
- SSH开发中 使用超链接到action 其excute方法会被执行两次 actual row count: 0; expected: 1
由于执行两次excute,所以在做删除操作的时候会出现 Batch update returned unexpected row count from update [0]; actual row c ...
- 885. Spiral Matrix III
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- Hibernate3核心API-Configuration类
- 代码实现:判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称
package com.loaderman.test; import java.io.File; import java.io.FilenameFilter; public class Test { ...