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,連續(xù)的O代表首項為1公差為1的等差數(shù)列,如果被X切斷,那么從首項就變成1,重新開始遞增。求被X分割的所有等差數(shù)列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++)
    {
             /*等差數(shù)列求和*/
        if (str[i]=='O')
        {               

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


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


    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,131評論 0 23
  • 年會的意義 年會:管理部門希望通過此增強凝聚力。 年會怎么組織 領(lǐng)導(dǎo)講話 娛樂節(jié)目表演 背景音樂 有獎問答關(guān)于組織...
    郭青耀閱讀 205評論 0 0
  • 自從考上大學(xué)以來逐漸開始晚睡 12點 1點 2點 到現(xiàn)在習(xí)以為常的3點 甚至不知不覺就能通宵 毫無壓力得看到清晨的...
    并沒有什么想法閱讀 184評論 0 1
  • 2017.06.13 杭州 雨 1.終于把蛀牙都補完啦; 2.找回了原本以為失蹤的雨傘; 3.晚上電話老媽氣氛還算不錯。
    笨寶寶閱讀 135評論 0 0
  • 文|凈名齋主 圖片來源|網(wǎng)絡(luò) 如意云紋寓吉祥,誰知出身乃爪杖。同出異名何至此?人生最怕入錯行。 “如意者,古...
    凈名齋主閱讀 494評論 4 4

友情鏈接更多精彩內(nèi)容