Go: package main

import (
	"bufio"
	"fmt"
	"os"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	var inning int

	fmt.Scanf("%d", &inning)

	score := make([]int, inning)

	for i := 0; i < inning; i++ {
		var outCount, runnerCount int

		for scanner.Scan() {
			event := scanner.Text()

			// HIT, HOMERUN, OUT
			switch event {
			case "HIT":
				runnerCount++
				if runnerCount > 3 {
					runnerCount--
					score[i]++
				}
			case "HOMERUN":
				score[i] += runnerCount + 1
				runnerCount = 0
			case "OUT":
				outCount++
			}

			if outCount == 3 {
				break
			}
		}
	}

	for _, v := range score {
		fmt.Println(v)
	}
}

C++: #include <iostream>
#include <random>

class Baseball{
private:
	//int event;
	int point_count;
	int out_count;
	int runner;

public:
	 Baseball(int p, int o, int r);
	 void singleHit();
	 void homerun();
	 void out();
	 void newInning();
	 int now_point();
	 int now_out();
	~ Baseball();
};

Baseball::Baseball(int p, int o, int r){
	point_count = p;
	out_count = o;
	runner = r;
}

Baseball::~Baseball(){	
}

void Baseball::singleHit(){
	if(runner < 3){
		++runner;
	}else if(runner == 3){
		++point_count;
	}
}

void Baseball::homerun(){
	point_count += runner+1;
	runner = 0;
}

void Baseball::out(){
	++out_count;
}

void Baseball::newInning(){
	point_count = 0;
	out_count = 0;
	runner = 0;
}

int Baseball::now_point(){
	return point_count;
}

int Baseball::now_out(){
	return out_count;
}

int main(){
	int num,event;
	Baseball baseball(0,0,0);

	// create random number
	std::random_device rand;
	std::mt19937 mt(rand());
	std::uniform_int_distribution<int> e(1, 3);

	std::cin >> num;

	for(int i=0; i<num; ++i){
		baseball.newInning();

		while(baseball.now_out() < 3){
			event = e(mt);

			if(event == 1){
				baseball.singleHit();
				std::cout << "HIT" << std::endl;

			}else if(event == 2){
				baseball.homerun();
				std::cout << "HOMERUN" << std::endl;

			}else if(event == 3){
				baseball.out();
				std::cout << "OUT" << std::endl;

			}
		}

		std::cout << baseball.now_point() << std::endl;
	}
	
}

C++: #include <iostream>
#include <string>

class Baseball{

private:
	int point;
	int out;
	int base;   //0:no runner 1:runner
				//home,third,second,first

public:

	Baseball(int fir_point, int fir_out, int fir_base){
		point = fir_point;
		out = fir_out;
		base = fir_base;
	}

	int get_point(){
		return point;
	}

	int get_out(){
		return out;
	}

	void hit(){
		base = (base * 10) + 1;
		
		if(base >= 1000){
			base -= 1000;
			point += 1;
		}
	}

	void home_run(){
		point += 1;
		for(int i = base; i > 0; i /= 10){
			point += i%10;
		}
		base = 0;
	}

	void add_out(){
		out += 1;
	}
};	

int main(){

	int N;
	std::cin >> N;

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

		Baseball baseball(0, 0, 0);

		while(true){
			std::string str;
			std::cin >> str;

			if(str == "HIT")
				baseball.hit();
			
			else if(str == "HOMERUN")
				baseball.home_run();

			else if(str == "OUT")
				baseball.add_out();
			
			if(baseball.get_out() == 3){
				std::cout >> baseball.get_point() >> std::endl;
				break;
			}
		}

	}

	return 0;
}

C++: #include <vector>
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		int out = 0;
		int base = 0;
		int score = 0;
		while (out < 3){
			char s[128];
			scanf("%s", s);
			if (s[1] == 'I'){
				if (base == 3) score++;
				else base++;
			}
			else if (s[1] == 'O'){
				score += base + 1;
				base = 0;
			}
			else out++;
		}
		printf("%d\n", score);
	}
}

C++: #include <iostream>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>

#define rep(x,to) for(int x=0;x<to;x++)
#define rep2(x,from,to) for(int x=from;x<to;x++)

using namespace std;



int main(void){

int n;
string ss;
	while(cin >> n){
		if(cin.eof()) break;
		rep(i,n){
			int ht=0, tt=0, ot=0;
			while(cin >> ss){
				if(ss=="OUT") ot++;
				if(ot > 2) break;
				if(ss=="HIT") ht++;
				if(ht > 3){ tt++; ht=3; }
				if(ss=="HOMERUN"){ tt += ht+1; ht=0;}
			}
			cout << tt << endl;
		}
	}
	return 0;
	
}

C++: #include <iostream>
#include <bitset>
#include <string>

class Baseball {
    // 各ビットがそれぞれの塁のランナーの有無を表現する
    std::bitset<4> runners;
    u_int _out, _score;
public:
    Baseball() :_out(0), _score(0) {};
    ~Baseball() = default;
    void hit();
    void homerun();
    void out() { ++_out; }
    void change();
    bool is_change() { return _out == 3; }
    u_int score() { return _score; }
};

void Baseball::hit() {
    runners <<= 1;  // ランナーを1つずつ進める
    runners.set(0); // 一塁にランナーを置く
    // ホームインしたかどうか
    if (runners.test(3)) {
        ++_score;
        runners.reset(3);
    }
}

void Baseball::homerun() {
    _score += runners.count() + 1;
    runners.reset(); // ランナーをリセット
}

void Baseball::change() {
    _out = _score = 0;
    runners.reset();
}

int main(void) {
    u_int n;
    std::string event;
    Baseball game;

    std::cin >> n;
    for (u_int i = 0; i < n; ++i) {
        do {
            std::cin >> event;
            if ("HIT" == event) {
                game.hit();
            } else if ("HOMERUN" == event) {
                game.homerun();
            } else if ("OUT" == event) {
                game.out();
            }
        } while (!game.is_change());

        std::cout << game.score() << std::endl;
        game.change();
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main() {
cin>>n;
for (int i = 0; i < n; ++i) {
int sum=0,out=0,finsum=0;
while(1){
cin>>s;
if(s=="HIT"){sum++;if(sum==4){finsum+=1;sum=3;}}
if(s=="OUT"){out++;if(out==3)break;}
if(s=="HOMERUN"){finsum+=sum;finsum++;sum=0;}
}
cout<<finsum<<endl;
}
}

C++: #include <cstdio>
#include <iostream>
#include <string>

#define rep2(x,from,to) for(int x=(from);(x)<(to);(x)++)
#define rep(x,to) rep2(x,0,to)

using namespace std;

int main() {
	int n;
	int base = 0, out = 0, count = 0;
	cin >> n;
	char str[10];
	while(1) {
		if(EOF == scanf("%s", str)) break;
		if(str == "OUT") {
			out++;
			cout << out << endl;
			if(out >= 3) {
				cout << count <<endl;
				count = out = base = 0;
				n--;
			}
		} else if(str == "HOMERUN") {
			count += base + 1;
			base = 0;
		} else {
			base++;
			if(base >= 4) {
				count++;
				base--;
			}
		}
	}
	cout << count << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;
int main(void){
    // Here your code !
    int n;
    cin >> n;
    vector<string> a(101);
    bool run[3]={false, false, false};
    int out=0, e=0;
    int point[50];
    int i=0;
    while(cin >> a[i]) {
        if(a[i]=="HIT") {
            if(run[2]) {
                point[e]++;
                run[2] = false;
            }
            if(run[1]) {
                run[2] = true;
                run[1] = false;
            }
            if(run[0]) {
                run[1] = true;
                run[0] = false;
            }
            run[0] = true;
        } else if(a[i]=="OUT") {
            out++;
            if(out==3) {
                out = 0;
                run[0] = false;
                run[1] = false;
                run[2] = false;
                e++;
            }
        } else {
            int k=0;
            if(run[0]) k++;
            if(run[1]) k++;
            if(run[2]) k++;
            run[0] = false;
            run[1] = false;
            run[2] = false;
            REP(i,k) {
                point[e]++;
            }
            point[e]++;
        }
        i++;
    }
    REP(i,n) {
        cout << point[i] << endl;
    }
    
}