UVA 1585 Score

There is an objective test result such as “OOXXOXXOOO”. An ‘O’ means a correct answer of a problem
and an ‘X’ means a wrong answer. The score of each problem of this test is calculated by itself and
its just previous consecutive ‘O’s only when the answer is correct. For example, the score of the 10th
problem is 3 that is obtained by itself and its two previous consecutive ‘O’s.
Therefore, the score of “OOXXOXXOOO” is 10 which is calculated by “1+2+0+0+1+0+0+1+2+3”.
You are to write a program calculating the scores of test results.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test
cases T is given in the first line of the input. Each test case starts with a line containing a string
composed by ‘O’ and ‘X’ and the length of the string is more than 0 and less than 80. There is no spaces
between ‘O’ and ‘X’.
Output
Your program is to write to standard output. Print exactly one line for each test case. The line is to
contain the score of the test case.
Sample Input
5
OOXXOXXOOO
OOXXOOXXOO
OXOXOXOXOXOXOX
OOOOOOOOOO
OOOOXOOOOXOOOOX
Sample Output
10
9
7
55
30
解题思路:在一个只有O和X的序列中,X代表0,连续的O代表首项为1公差为1的等差数列,如果被X切断,那么从首项就变成1,重新开始递增。求被X分割的所有等差数列an的和。

#include <stdio.h>
#include<string.h>
#define MAX 80

int main()
{
    /* code */
    int T,num,len; 
    scanf("%d",&T);
    char str[MAX];
    while(T--){
    num=1;
    scanf("%s",str);
    len=strlen(str);
    int sum=0;//定义初始总和为0
    for (int i = 0; i < len; i++)
    {
             /*等差数列求和*/
        if (str[i]=='O')
        {               

            sum+=num;
            num++;
        }
        else
            num=1;


    }
        printf("%d\n",sum);


    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,494评论 0 23
  • 年会的意义 年会:管理部门希望通过此增强凝聚力。 年会怎么组织 领导讲话 娱乐节目表演 背景音乐 有奖问答关于组织...
    郭青耀阅读 1,454评论 0 0
  • 自从考上大学以来逐渐开始晚睡 12点 1点 2点 到现在习以为常的3点 甚至不知不觉就能通宵 毫无压力得看到清晨的...
    并没有什么想法阅读 1,328评论 0 1
  • 2017.06.13 杭州 雨 1.终于把蛀牙都补完啦; 2.找回了原本以为失踪的雨伞; 3.晚上电话老妈气氛还算不错。
    笨寶寶阅读 1,135评论 0 0
  • 文|净名斋主 图片来源|网络 如意云纹寓吉祥,谁知出身乃爪杖。同出异名何至此?人生最怕入错行。 “如意者,古...
    净名斋主阅读 3,184评论 4 4

友情链接更多精彩内容