📦 Changgo/🍣 EDOC

BOJ 백준 20170번: Commemorative Dice

선달 2021. 10. 1. 23:54
반응형

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

 

20170번: Commemorative Dice

Since the year 2000, an ICPC regional contest has been held every year in Korea. To commemorate the 21st regional contest this year, it is decided to make a dice. The commemorative dice is a regular cube with a positive number written on each of its sides

www.acmicpc.net

 

문제

Since the year 2000, an ICPC regional contest has been held every year in Korea. To commemorate the 21st regional contest this year, it is decided to make a dice. The commemorative dice is a regular cube with a positive number written on each of its sides like an ordinary dice; however, the six numbers are not necessarily to be 1, 2, 3, 4, 5, 6 but just their sum is 21.

The dice can be used in various ways. For example, two people can play a game as follows: Each of the two picks one out of the many dice and then rolls the dice. The winner is the one who tosses a bigger number. It is important which dice to choose in this game because once the dice are set, the probability of one winning against the other is determined. Suppose that KyungYong chooses the dice shown in the figure below left and TaeCheon chooses the dice shown in the figure below right. Then, KyungYong wins when and only when TaeCheon tosses number 1, so the probability that KyungYong wins is 2/3.

Given the dice of the first and second players, write a program to calculate the probability of the first player winning.

입력

Your program is to read from standard input. The input consists of two lines. The first line contains six positive integers that are written on the sides of the dice of the first player. Also, the second line contains six positive integers that are written on the sides of the dice of the second player. The six integers given in a line add up to 21 and are separated by a single space.

 

출력

Your program is to write to standard output. Print exactly one line that contains an irreducible fraction representing the probability of the first player winning. A fraction should consist of a numerator displayed before a slash and a non-zero denominator displayed after the slash. There are no spaces either before or after the slash. Note that an irreducible fraction refers to a fraction in which the numerator and denominator are integers that have no other common divisors than 1.

 

풀이

주사위 눈을 하나씩 비교해서 이기는 경우의 수를 구한다

..까지의 풀이는 간단하다. ..하지만

사실 이 문제의 포인트는 c++로 기약분수를 어떻게 만드느냐..!!

 

이 문제에서는 분모가 36으로 고정되어 있기 때문에 한결 편하다

 

36의 약수중 큰 것부터 이긴 수 w 를 나누어 떨어뜨리는지 확인하고,

나누어 떨어진다면 36과 w를 둘 다 해당 공약수로 나눠서 각각 최종 분모와 분자로 쓴다

 

#include <iostream>

using namespace std;

int main () {
    
    //입력
    int a[6], b[6];
    for(int i=0; i<6; i++){
        cin >> a[i];
    }
    for(int i=0; i<6; i++){
        cin >> b[i];
    }

    //우승하는 경우의 수 계산
    int win=0;
    for(int i=0; i<6; i++){
        for(int j=0; j<6; j++){
            if(a[i] > b[j])
                win++;
        }
    }
    
    //기약분수 만들기
    int m = 36;
    int arr[] = {36,18,12,9,6,4,3,2};
    for(int i=1; i<8; i++){
        if(win%arr[i] == 0) {
            win /= arr[i];
            m /= arr[i];
            break;
        }
    }
    
    //출력
    cout << win << "/" << m;
    
}

 

반응형