您的位置:首页 > 其它

UVA1585 得分score

2017-11-08 23:52 260 查看

算法竞赛入门经典第二版 P57 习题3-1

感悟:第一次写就直接写出来了,此题较容易。

思路:对输入的字符串数组逐一判断。并定义一个中间变量,来形容O的递增情况。遇O递增,遇X归为0。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define maxn 85

int main()

{   int tot=0,kase=0;

    int T;

    scanf("%d",&T);

    char s[maxn];

    while(T--){
scanf("%s",s);
    int n=strlen(s);

        for(int i=0;i<n;i++){

            if(s[i]=='O')++kase;

            if(s[i]=='X')kase=0;

            tot+=kase;

        }

    }

    printf("%d\n",tot);

    return 0;

}

另附上个人做题心得:可以在for循环内加个printf表示Kase的变化情况,方便找出代码错误,以及验证代码正确性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  刘汝佳 算法 uva