Skip to content

Hello World for U

题目链接1031 Hello World for U (20 分)

题干大意

将给定的字符串,按照U 型输出

思路

计算出n1n2n3,然后按行输出即可。由n1 = n3且不大于n2则有n1 = n3 = (n + 2) / 3的下整。

AC代码

C++
#include <iostream>
using namespace std;

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