//大概是一道水题吧...我却做了半天...而且明显不是最优解...我好菜喔 #include <iostream> #include <string> using namespace std; int main(void) { string s; while (cin >> s) { int score = 0; /* //旧方法...太蠢了... int j; for (int i = 0; i < s.length();) { //如果找到O if (s[i] == 'O') { int count = 1; //从找到O处开始遍历,直到找到X或到结尾。这时j存储着这一串连续O的最后一个O的index for (j = i; j < s.length(); j++) { if (s[j] == 'O') { score += count; count++; } else break; } //将下一个X的地址赋给i i = j + 1; } //如果找到的不是O,则+1后继续循环 else i++; } */
int Val = 0; for (int i = 0; i < s.length(); i++) { //这个思想是对每一个字符都进行赋值。遇到X则清零 if (s[i] == 'O') Val++; else Val = 0; score += Val; } cout << score << endl; } }