跳到正文

题解

一个简便的方法

happybob约 1 分钟
原文
文章信息与版本

文章信息

发布
2021年4月30日
收录
2026年7月31日
更新
2026年11月9日
篇幅
约 1 分钟

版本

  1. v1当前

你们都判断整个字符串,但是因为三个的首字母都不同,用首字母判断即可,虽然这可能影响不到最终的分数,但是题目做多了这种优化感觉自己就出来了,比赛时也少打一些,能优化优化,养成好习惯、好码风。这道题用 switch case 会好一些,根据题目意思模拟即可。

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

int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	for (register int i = 1; i <= n; i++)
	{
		string a, b, c;
		cin >> a >> b >> c;
		switch (a[0])
		{
		case 'B':
			cout << c[0];
			break;
		case 'M':
			cout << c[(c.length() - 1) >> 1];
			break;
		default:
			cout << c[c.length() - 1];
			break;
		}
	}
	cout << endl;
	return 0;
}
原文