UVA 1585 Score

Problem

https://uva.onlinejudge.org/external/15/1585.pdf

Thinking

這題遇到連續O則會開始加總一個會慢慢遞增的數字, 此數字再遇到X時變回1, 此題很簡單, 會記錄此題的原因是我不小心把題目想得太複雜了

#include
using namespace std;

void solve(string str)
{
    
    int sum = 0;
    int increment = 1;
    for(int i = 0 ; i < str.length() ; i++)
    {
        if(str[i] == 'O'){
            sum += increment;
            increment++;
        }
            
        if(str[i] == 'X')
            increment = 1;
    }
    
    cout << sum << endl;
}

int main()
{
    int testcase;
    string str;
    
    cin >> testcase;
    while(testcase--)
    {
        cin >> str;
        solve(str);
    }
     return 0;
}

你可能感兴趣的:(UVA 1585 Score)