一开始怎么都不理解题目,上网看了这个代码,顿时惭愧啊;
将输入的两行当作一个 pair ,先操作第二行, 再操作第一行; 其他的就是不太熟悉 getline() 的用法了,它可以将一行字符串读入(包含空格)直至遇见回车符
另assign 的用法见:http://blog.csdn.net/chenhq1991/article/details/7799172
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
bool checkEmptyLine(string str)
{
for (int i = 0; i < str.size(); i++)
{
if (str[i] != ' ')
return true;
}
return false;
}
int main()
{
string firstStr;
string secondStr;
while (getline(cin, firstStr) && getline(cin, secondStr))
{
if (secondStr != "" && checkEmptyLine(secondStr))
{
reverse(secondStr.begin(), secondStr.end());
int middleIndex = secondStr.size() / 2;
string tempFirst;
tempFirst.assign(secondStr.begin(), secondStr.begin() + middleIndex);
string tempSecond;
tempSecond.assign(secondStr.begin() + middleIndex, secondStr.end());
secondStr = tempSecond + tempFirst;
cout << secondStr << endl;
}
if (firstStr != "" && checkEmptyLine(firstStr))
{
reverse(firstStr.begin(), firstStr.end());
int middleIndex = firstStr.size() / 2;
string tempFirst;
tempFirst.assign(firstStr.begin(), firstStr.begin() + middleIndex);
string tempSecond;
tempSecond.assign(firstStr.begin() + middleIndex, firstStr.end());
firstStr = tempSecond + tempFirst;
cout << firstStr << endl;
}
firstStr.clear();
secondStr.clear();
}
return 0;
}
转自:http://blog.csdn.net/jaychouaa1288/article/details/6943025