注意输入最后一行可能没有回车换行,用getline()得到的字符串没有‘\0’。
#include <iostream>
#include <string>
using namespace std;
int getScore(string str)
{
int x = 0;
int res = 0;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == 'X')
{
x = 0;
}
else if (str[i] == 'O')
{
x++;
res = res + x;
}
}
return res;
}
int main()
{
int T;
string str;
cin >> T;
getline(cin, str);
while (T-- > 0)
{
getline(cin, str);
cout << getScore(str) << endl;
}
return 0;
}