得分

题目

给出一个由O和X组成的串(长度为1-80),统计得分。每个O的得分为目前连续出现的O的个数,X的得分为0.

例如,OOXXOXXOOO的得分为 1+2+0+0+1+0+0+1+2+3。

分析

用一个变量记录连续的O的数量,遇到X则置为1

c实现

#include<stdio.h>
#include<string.h>
#define maxn 100

char s[maxn];
int main()
{
    scanf("%s",s);
    //用a来标记连续O的数量 
    int sum=0,a=1;
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]=='O')
        {
            sum += (a++);
        }else{
            a=1;
        }
    }
    printf("%d",sum);
    return 0;
}
原文地址:https://www.cnblogs.com/Vincent-yuan/p/12941459.html