[백준 13987] Six Sides (C#, C++) - soo:bak
작성일 :
문제 링크
설명
두 개의 주사위를 굴릴 때 첫 번째 주사위가 이길 확률을 구하는 문제입니다.
접근법
먼저 두 주사위의 모든 눈 조합을 확인해 승리 횟수를 셉니다.
다음으로 같은 값인 경우는 다시 굴리므로 승패에서 제외합니다.
이후 첫 번째가 이긴 횟수를 승패가 결정된 경우의 수로 나눕니다.
마지막으로 소수 다섯째 자리까지 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Globalization;
class Program {
static void Main() {
var a = Array.ConvertAll(Console.ReadLine()!.Split(), int.Parse);
var b = Array.ConvertAll(Console.ReadLine()!.Split(), int.Parse);
var win = 0;
var total = 0;
for (var i = 0; i < 6; i++) {
for (var j = 0; j < 6; j++) {
if (a[i] == b[j]) continue;
total++;
if (a[i] > b[j]) win++;
}
}
var prob = (double)win / total;
Console.WriteLine(prob.ToString("0.00000", CultureInfo.InvariantCulture));
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
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;
int total = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (a[i] == b[j]) continue;
total++;
if (a[i] > b[j]) win++;
}
}
double prob = (double)win / total;
cout.setf(ios::fixed);
cout << setprecision(5) << prob << "\n";
return 0;
}