UVA 1585 Score (c++)(字符串处理)

    题目大意是给一个由O和X组成的字符串,而这个字符串的得分是由连续的O的数目来统计的。比如OOXXOOXXOO的得分就是1+2+0+0+1+2+0+0+1+2=9。了解了题目大意就好解决了。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <ctype.h>
using namespace std ;

const int maxn = 100005 ;

int main(){
    int t ;
    scanf("%d" , &t) ;
    getchar() ;
    while ( t -- ){
        int tot = 0 ;
        int ans = 0 ;
        char ch ;
        while ( (ch = getchar()) != '\n' ){
            if ( ch == 'O' ) { tot ++ ; ans += tot ; }   ///遇到O则tot先加,再把结果加到ans上
            else tot = 0 ;        ///遇到X将tot清零
        }
        printf("%d\n" , ans) ;
    }
    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/CoLpac/article/details/81163878