1. 程式人生 > >The score of 'O' and 'X'

The score of 'O' and 'X'

題目描述

注意要點:

  • 使用strlen函式注意加標頭檔案#inlcude <cstring>
  • 迴圈巨集定義for迴圈#define _for(i,a,b) for(int i=(a);i<(b);++i)

程式碼實現

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#define _for(i,a,b) for(int i=(a);i<(b);++i)  //迴圈巨集定義for迴圈
using namespace std;

int main() {
    int T;  //測試樣例的組數
    char buf[128];
    scanf("%d\n",&T);
    while(T--){
        gets(buf);//此處是用gets函式讀入
        int cnt=0,sum=0,sz=strlen(buf); //在CPP中需要引用#include <cstring>
        _for(i,0,sz){
            if(buf[i]=='O')
                sum+=(++cnt);//sum是累加的,cnt遇到字元X會清0
                else cnt = 0;
        }
        printf("%d\n",sum);
    }
    return 0;
}