0
点赞
收藏
分享

微信扫一扫

pta L1 - 059 敲笨钟

爱动漫建模 2022-03-25 阅读 76
c++

跟上一题的思路大体相同,不过在写这道题的时候,发现了一个很巧妙的事情,具体写到代码里面了

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string hunger;
	string temp = { " qiao ben zhong." };
	int n;
	cin >> n;
	getchar();
	for (int i = 0; i < n; i++)
	{
		getline(cin, hunger);
		int a = hunger.find(",");
		int b = hunger.find(".");
		auto it1 = hunger.begin() + a;
		auto it2 = hunger.begin() + b;
		if (*(it1 - 3) == 'o' && *(it1 - 2) == 'n' && *(it1 - 1) == 'g' && *(it2 - 3) == 'o' && *(it2 - 2) == 'n' && *(it2 - 1) == 'g')
		{
			auto iter = hunger.rbegin();
			int flag = 0 , j = 0;
			while (flag != 3)
			{
				if (*(iter++) == ' ')
					flag++;
				j++;
			}
			//hunger.erase(iter);          此处注意,若是迭代器为反向迭代器,则不能用删除迭代器的方式来删除元素,而从begin开始的迭代器可以
			hunger.erase(hunger.size() - j);
			hunger.insert(hunger.size(), temp);
			cout << hunger << endl;
		}
		else
			cout << "Skipped" << endl;
		hunger.clear();
	}
	return 0;
}
举报

相关推荐

0 条评论