[백준] 구현 13224번: Chop Cup

C.K. ·2022년 6월 9일
0

baekjoon

목록 보기
13/67

문제

Oisín is amateur magician and is big fan of Chop Cup routine which involves three cups face down and one ball underneath one of the cups. He's only started to practice the trick and wants to test out if you can follow where the ball is without any tricks or slight of hand.

The ball starts under the leftmost cup and Oisín then swaps two cups in one of three possible ways a number of times.

What Oisin doesn't realise is that you are recording the moves with your phone using the letters A, B or C and are going to use a simple program to determine where the ball is. Write that program.

입력

The first and only line contains a string of at most 50 characters, Oisín's moves.

Each of the characters is 'A', 'B' or 'C' (without quote marks).

출력

Output the index of the cup under which the ball is: 1 if it is under the left cup, 2 if it is under the middle cup or 3 if it is under the right cup.

Approach

  • 컵들을 저장하는 배열을 만들고 공이 있는 컵의 인덱스를 1로 두고 나머지 컵들의 인덱스는 0으로 둔다. 공의 위치가 바뀌는 순서를 입력받고 각 letter에 맞춰 배열에서 값들을 swap해준다. 마지막에 공이 있는 컵, 즉 값이 1인 인덱스를 출력한다

Source Code

#include <iostream>
#include <string>
using namespace std;

// swap함수 구현
void swap(int arr[], int a, int b)
{
    int temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
}


int main() {
    
    int balls[3] = {1,0,0}; // 컵들을 저장하는 배열 
    						// 시작할 때 공의 위치는 leftmost cup
    
    string input; // 공의 위치가 바뀌는 순서를 입력받는다
    cin >> input;
    
    // 각 letter 확인, 그리고 letter에 따라 배열의 값을 swap해준다
    for (int i = 0; i < input.length(); i++)
    {
        if (input[i] == 'A')
        {
            swap(balls, 0, 1);
        }
        else if (input[i] == 'B')
        {
            swap(balls, 1, 2);
        }
        else if (input[i] == 'C')
        {
            swap(balls, 0, 2);
        }
    }

	// swap이 끝난 후 공이 있는 컵을 출력 or 배열에서 값이 1인 인덱스 출력
    for (int j = 0; j < 3; j++)
    {
        if (balls[j] == 1)
            cout << j + 1 << endl;
    }
    return 0;
}
profile
1일 1알고리즘

1개의 댓글

comment-user-thumbnail
2022년 6월 9일

ggg

답글 달기