题目

1.题目分析
一道简单的字符串操作题,直接套用C++中字符串对应的操作即可
2.做题思路
-
使用
while循环读入字符串inpt为.时停止读入 -
对
inpt字符串执行inpt.append(1,' ')意思是在读入的字符串后加一个空格,因为cin会排掉空格 -
定义一个
str字符串用来储存答案 -
对
str字符串执行str.insert(0, inpt)即将输入内容添加至str字符串最前面 -
输出
str即可
3.复杂度计算
由于只需要循环长度次,所以时间复杂度为 $O(n)$ 是完全不会超的
4.完整代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
string inpt;
while(true)
{
cin >> inpt ;
if(inpt == ".")
{
break;
}
inpt.append(1, ' ');
str.insert(0, inpt);
}
cout << str ;
return 0;
}
写在最后
有问题请及时评论,我会做出对应的修改!