紫薯P57 习题3-1
一、题意
有kase个OOXX串
每个O的得分为目前连续出现的O的个数,X不得分。
请求每个串的得分。
二、解析
每个串扫一遍即可,用cnt记录当前连续O的个数,ans记录得分。
三、代码
#include <iostream>
#include <string>
using namespace std;
int kase;
int main() {
cin >> kase;
while(kase --) {
string str;
cin >> str;
int cnt = 0, ans = 0;
for(char ch : str) {
if(ch == 'O') cnt ++, ans += cnt;
else cnt = 0;
}
cout << ans << endl;
}
}四、归纳
- 当题目简单时,就是比拼速度的时候了
水了一题

京公网安备 11010502036488号