题解 第二章暴力求解| #Hello World For U#
Hello World for U
http://www.nowcoder.com/practice/c6e414fddd7c401887c350c9cc41f01b
/**
* @file StringForU.cpp
* @author Shay (837328426@qq.com)
* @brief 将字符串变成一个最接近U的形状
* 下面比上面长一个字符
* @date 2022-01-15
*/
#include <iostream>
#include <vector>
#include <string>
#define MAXSIZE 80
using namespace std;
int main()
{
string s;
while (cin >> s)
{
int N = s.size();
int N_Vert = (N - 1) / 3 + 1;
//不要用sizeof(string),此句出错
// https://blog.csdn.net/u014082714/article/details/45334513
int N_Bott = (N + 2 - 2 * N_Vert);
string SpaceString(N_Bott - 2, ' ');
string BottomString(&s[N_Vert - 1], &s[N_Vert + N_Bott - 1]);
for (int i = 0; i < N_Vert - 1; i++)
{
cout << s[i] //*(s.begin() + i)
<< SpaceString
<< s[N - i - 1] //*(s.end() - i - 1)
<< endl;
//注意s.end()所指向的是字符串最末尾的NULL
//故需要继续-1
}
cout << BottomString << endl;
}
}
王道机试指南刷题 文章被收录于专栏
计划刷完这本书