PAT Advanced 1031 Hello World for U (20分)

Given any string of N (≥) 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 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 } 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:

helloworld!

Sample Output:

h   !
e   d
l   l
lowor

这道题目考察了格式化字串,我们需要先补2个位置,除以三,进行计算两边的数量,最后再计算中间的

#include <iostream>
using namespace std;
int main() {
    string str;
    getline(cin, str);
    int row, N = str.length(), recol;
    row = (N + 2) / 3 ;
    recol = (N + 2) / 3 + (N + 2) % 3 - 2;
    for(int i = 0; i < row; i++){
        cout << str[i];
        for(int j = 0; j < recol; j++)
            if(i != row - 1) cout << " ";
            else cout << str[j + row];
        cout << str[N - i -1] << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/littlepage/p/12228395.html

时间: 2024-10-08 08:00:00

PAT Advanced 1031 Hello World for U (20分)的相关文章

1031 Hello World for U (20 分)

1031 Hello World for U (20 分) Given any string of N (≥) 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, s

PAT Advanced 1055 The World&#39;s Richest (25分)

Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the n

PAT乙级:1053 住房空置率 (20分)

PAT乙级:1053 住房空置率 (20分) 题干 在不打扰居民的前提下,统计住房空置率的一种方法是根据每户用电量的连续变化规律进行判断.判断方法如下: 在观察期内,若存在超过一半的日子用电量低于某给定的阈值 e,则该住房为"可能空置": 若观察期超过某给定阈值 D 天,且满足上一个条件,则该住房为"空置". 现给定某居民区的住户用电量数据,请你统计"可能空置"的比率和"空置"比率,即以上两种状态的住房占居民区住房总套数的百分

pat 1124 Raffle for Weibo Followers(20 分)

1124 Raffle for Weibo Followers(20 分) John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers on Weibo -- that is, he would select winners from every N followers who forwarded his post, and give away gifts.

PAT 乙级 1074 宇宙无敌加法器 (20 分)

1074 宇宙无敌加法器 (20 分) 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为"PAT数".每个 PAT 星人都必须熟记各位数字的进制表,例如"--0527"就表示最低位是 7 进制数.第 2 位是 2 进制数.第 3 位是 5 进制数.第 4 位是 10 进制数,等等.每一位的进制 d 或者是 0(表示十进制).或者是 [2,9] 区间内的整数.理论上这个进制

PAT乙级1023. 组个最小数(20 分)

1023 组个最小数(20 分) 给定数字 0-9 各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就是 10015558. 现给定数字,请编写程序输出能够组成的最小的数. 输入格式: 输入在一行中给出 10 个非负整数,顺序表示我们拥有数字 0.数字 1.……数字 9 的个数.整数间用一个空格分隔.10 个数字的总个数不超过 50,且至少拥有 1 个非 0 的数字

pat 1058 A+B in Hogwarts(20 分)

1058 A+B in Hogwarts(20 分) If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it's easy enough.

PAT 甲级 1027 Colors in Mars (20 分)

1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the

PAT 甲级 1054 The Dominant Color (20 分)

1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A st