본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 5751 : Head or Tail

반응형

https://www.acmicpc.net/problem/5751

 

5751번: Head or Tail

John and Mary have been friends since nursery school. Since then, they have shared a playful routine: every time they meet, they play Head or Tail with a coin, and whoever wins has the privilege of deciding what they are going to play during the day. Mary

www.acmicpc.net

구현문제였습니다.

📕 풀이방법

📔 입력 및 초기화

test case n을 선언 후 while loop를 수행하며 입력해줍니다. 이 후 n개의 수를 입력해줍니다.johnWin, maryWin을 선언해줍니다.

📔 풀이과정

n개의 수를 입력하며 0이면 maryWin, 1이면 johnWin을 1더해줍니다.

📔 정답출력

정답을 출력해줍니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;
int n;
int main(){
    while(1){
        int johnWin = 0, maryWin = 0;
        cin >> n;
        if(!n) break;
        for(int i = 0, x; i < n; i++){
            cin >> x;
            if(!x) maryWin++;
            else johnWin++;
        }
        printf("Mary won %d times and John won %d times\n", maryWin, johnWin);
    }
}