In a dice game each player rolls five dice (faces 1 to 6) once.
The hand of the five faces is the highest of the following eight hands that fits them. The higher a hand is in the list, the stronger it is.
- FIVE: all five dice show the same face
- FOUR: four dice show the same face
- FULLHOUSE: three dice show one face and the other two show another, equal face
- STRAIGHT: the five faces are 1 2 3 4 5 or 2 3 4 5 6 (in any order)
- TRIPLE: three dice show the same face and the other two differ from it and from each other
- TWOPAIR: two different faces appear twice each
- PAIR: exactly one pair of dice shows the same face
- NOTHING: every other case
The first line gives the number of players n, and each of the next n lines gives one player's five faces separated by spaces. Players are numbered from 1 in order.
Print each player's hand name in upper case, one per line.
On the last line print the number of the winner.
The player with the strongest hand wins; if the hands are equal, the larger sum of the five faces wins. If that is also equal, the smaller player number wins.
Try making the hands an enum with eight values from weakest to strongest, and compare hands by the size of their enum values.
Input
1 ≤ n ≤ 1,000, each face is an integer from 1 to 6
Output
n+1 lines: each player's hand name, then the winner's number
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 6 languages
This problem accepts only Python, TypeScript, Java, C#, C and C++, the languages that have enums. JavaScript has no enum syntax and neither does Craykon. If you declare the hands from weakest to strongest, the size of the value is the strength, so "is it stronger" is a single >, but comparing enums by size differs by language. A plain Python Enum does not know < or > and raises TypeError. Make it an IntEnum or compare .value. TypeScript numeric enums and C# enums compare directly with >, and a C++ enum class supports < and > between values of the same type (you need static_cast only when mixing with integers). A Java enum cannot use >; use compareTo, which compares by declaration order (ordinal()). A C enum is just named int constants, so plain > works. Printing the name differs too: Python h.name, the TS reverse mapping Hand[h], Java toString(), C# ToString().ToUpperInvariant() (turning the declared FullHouse upper case), a name table array in C, and a switch returning the name in C++. Five traps: ① Printing a Python IntEnum value directly with print(h) gives a number like 5 instead of the name since 3.11 (a plain Enum gives Hand.FULLHOUSE). ② Using > on a plain Enum stops with a TypeError at run time. ③ Ask from the strongest hand down. If you ask "are three the same" before full house, 3 3 3 5 5 becomes TRIPLE. ④ "largest face - smallest face == 4" alone is not a straight, because 1 1 1 3 5 also satisfies it. All five faces must differ (then they are consecutive only when the missing face is 1 or 6). ⑤ With equal hand and sum the earlier player wins, so replace only on >, never >=. And if you declared the enum strongest first (FIVE as 0), the comparison has to be flipped too.
Pythonmain.py · 48 lines
import sysfrom collections import Counterfrom enum import IntEnumclass Hand(IntEnum): # 약한 것부터 적어서 값이 클수록 센 족보가 된다(IntEnum이라 > 로 비교할 수 있다) NOTHING = 0 PAIR = 1 TWOPAIR = 2 TRIPLE = 3 STRAIGHT = 4 FULLHOUSE = 5 FOUR = 6 FIVE = 7def classify(dice): counts = sorted(Counter(dice).values(), reverse=True) # 같은 눈이 몇 개씩인가, 많은 것부터 if counts[0] == 5: return Hand.FIVE if counts[0] == 4: return Hand.FOUR if counts == [3, 2]: return Hand.FULLHOUSE if sorted(dice) in ([1, 2, 3, 4, 5], [2, 3, 4, 5, 6]): return Hand.STRAIGHT if counts[0] == 3: return Hand.TRIPLE if counts == [2, 2, 1]: return Hand.TWOPAIR if counts[0] == 2: return Hand.PAIR return Hand.NOTHINGlines = sys.stdin.read().split("\n")n = int(lines[0])best = None # (족보, 합, 번호)out = []for i in range(1, n + 1): dice = list(map(int, lines[i].split())) h = classify(dice) out.append(h.name) # print(h)는 IntEnum이라 "7"처럼 수가 나온다 if best is None or (h, sum(dice)) > (best[0], best[1]): best = (h, sum(dice), i)out.append(str(best[2]))print("\n".join(out))
TypeScriptmain.ts · 48 lines
import { readFileSync } from "node:fs";// 약한 것부터 적으면 값(0, 1, 2…)이 클수록 센 족보가 된다enum Hand { NOTHING, PAIR, TWOPAIR, TRIPLE, STRAIGHT, FULLHOUSE, FOUR, FIVE,}function classify(dice: number[]): Hand { const cnt: number[] = new Array<number>(7).fill(0); // cnt[눈] = 그 눈이 나온 주사위 수 for (const d of dice) cnt[d]++; const most = Math.max(...cnt); const pairs = cnt.filter((c) => c === 2).length; const triple = cnt.includes(3); if (most === 5) return Hand.FIVE; if (most === 4) return Hand.FOUR; if (triple && pairs === 1) return Hand.FULLHOUSE; // 다섯 눈이 모두 다르면 빠진 눈은 하나: 1이나 6이 빠졌을 때만 이어진다 if (most === 1 && (cnt[1] === 0 || cnt[6] === 0)) return Hand.STRAIGHT; if (triple) return Hand.TRIPLE; if (pairs === 2) return Hand.TWOPAIR; if (pairs === 1) return Hand.PAIR; return Hand.NOTHING;}const lines: string[] = readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const out: string[] = [];let bestHand = Hand.NOTHING, bestSum = -1, winner = 0;for (let i = 1; i <= n; i++) { const dice = lines[i].trim().split(/\s+/).map(Number); const h = classify(dice); const sum = dice.reduce((a, b) => a + b, 0); out.push(Hand[h]); // h는 수라서 이름은 역방향 표 Hand[h]로 꺼낸다 if (h > bestHand || (h === bestHand && sum > bestSum)) { bestHand = h; bestSum = sum; winner = i; }}out.push(String(winner));console.log(out.join("\n"));
JavaMain.java · 55 lines
import java.io.*;import java.util.*;public class Main { // 약한 것부터 적으면 선언 차례(ordinal)가 곧 세기 차례다 enum Hand { NOTHING, PAIR, TWOPAIR, TRIPLE, STRAIGHT, FULLHOUSE, FOUR, FIVE } static Hand classify(int[] dice) { int[] cnt = new int[7]; // cnt[눈] = 그 눈이 나온 주사위 수 for (int d : dice) cnt[d]++; int most = 0, pairs = 0; boolean triple = false; for (int c : cnt) { most = Math.max(most, c); if (c == 2) pairs++; if (c == 3) triple = true; } if (most == 5) return Hand.FIVE; if (most == 4) return Hand.FOUR; if (triple && pairs == 1) return Hand.FULLHOUSE; // 다섯 눈이 모두 다르면 빠진 눈은 하나: 1이나 6이 빠졌을 때만 이어진다 if (most == 1 && (cnt[1] == 0 || cnt[6] == 0)) return Hand.STRAIGHT; if (triple) return Hand.TRIPLE; if (pairs == 2) return Hand.TWOPAIR; if (pairs == 1) return Hand.PAIR; return Hand.NOTHING; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); StringBuilder sb = new StringBuilder(); Hand bestHand = null; int bestSum = -1, winner = 0; for (int i = 1; i <= n; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); int[] dice = new int[5]; int sum = 0; for (int k = 0; k < 5; k++) { dice[k] = Integer.parseInt(st.nextToken()); sum += dice[k]; } Hand h = classify(dice); sb.append(h).append('\n'); // enum의 toString()은 이름 // enum의 compareTo는 선언 차례로 비교한다 if (bestHand == null || h.compareTo(bestHand) > 0 || (h == bestHand && sum > bestSum)) { bestHand = h; bestSum = sum; winner = i; } } sb.append(winner).append('\n'); System.out.print(sb); }}
C#Main.cs · 50 lines
using System;using System.Linq;using System.Text;// 약한 것부터 적으면 값(0, 1, 2…)이 클수록 센 족보가 된다enum Hand { Nothing, Pair, TwoPair, Triple, Straight, FullHouse, Four, Five }class Program{ static Hand Classify(int[] dice) { var cnt = new int[7]; // cnt[눈] = 그 눈이 나온 주사위 수 foreach (int d in dice) cnt[d]++; int most = cnt.Max(); int pairs = cnt.Count(c => c == 2); bool triple = cnt.Contains(3); if (most == 5) return Hand.Five; if (most == 4) return Hand.Four; if (triple && pairs == 1) return Hand.FullHouse; // 다섯 눈이 모두 다르면 빠진 눈은 하나: 1이나 6이 빠졌을 때만 이어진다 if (most == 1 && (cnt[1] == 0 || cnt[6] == 0)) return Hand.Straight; if (triple) return Hand.Triple; if (pairs == 2) return Hand.TwoPair; if (pairs == 1) return Hand.Pair; return Hand.Nothing; } static void Main() { int n = int.Parse(Console.ReadLine()!.Trim()); var sb = new StringBuilder(); Hand bestHand = Hand.Nothing; int bestSum = -1, winner = 0; for (int i = 1; i <= n; i++) { int[] dice = Console.ReadLine()!.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); Hand h = Classify(dice); int sum = dice.Sum(); sb.Append(h.ToString().ToUpperInvariant()).Append('\n'); // ToString()은 "FullHouse" if (h > bestHand || (h == bestHand && sum > bestSum)) { bestHand = h; bestSum = sum; winner = i; } } sb.Append(winner).Append('\n'); Console.Write(sb); }}
C++main.cpp · 63 lines
#include <algorithm>#include <array>#include <iostream>#include <string>// 약한 것부터 적으면 선언 차례가 곧 세기 차례다. enum class끼리는 < > 로 바로 비교된다enum class Hand { Nothing, Pair, TwoPair, Triple, Straight, FullHouse, Four, Five };// enum class는 자기 이름을 모르니 switch로 돌려준다std::string name(Hand h) { switch (h) { case Hand::Nothing: return "NOTHING"; case Hand::Pair: return "PAIR"; case Hand::TwoPair: return "TWOPAIR"; case Hand::Triple: return "TRIPLE"; case Hand::Straight: return "STRAIGHT"; case Hand::FullHouse: return "FULLHOUSE"; case Hand::Four: return "FOUR"; case Hand::Five: return "FIVE"; } return "";}Hand classify(const std::array<int, 5>& dice) { std::array<int, 7> cnt{}; // cnt[눈] = 그 눈이 나온 주사위 수 for (int d : dice) cnt[d]++; int most = *std::max_element(cnt.begin(), cnt.end()); int pairs = std::count(cnt.begin(), cnt.end(), 2); bool triple = std::count(cnt.begin(), cnt.end(), 3) > 0; if (most == 5) return Hand::Five; if (most == 4) return Hand::Four; if (triple && pairs == 1) return Hand::FullHouse; // 다섯 눈이 모두 다르면 빠진 눈은 하나: 1이나 6이 빠졌을 때만 이어진다 if (most == 1 && (cnt[1] == 0 || cnt[6] == 0)) return Hand::Straight; if (triple) return Hand::Triple; if (pairs == 2) return Hand::TwoPair; if (pairs == 1) return Hand::Pair; return Hand::Nothing;}int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; Hand bestHand = Hand::Nothing; int bestSum = -1, winner = 0; for (int i = 1; i <= n; i++) { std::array<int, 5> dice; int sum = 0; for (int& d : dice) { std::cin >> d; sum += d; } Hand h = classify(dice); std::cout << name(h) << "\n"; if (h > bestHand || (h == bestHand && sum > bestSum)) { bestHand = h; bestSum = sum; winner = i; } } std::cout << winner << "\n";}
Cmain.c · 51 lines
#include <stdbool.h>#include <stdio.h>// 약한 것부터 적으면 값(0, 1, 2…)이 클수록 센 족보가 된다enum Hand { NOTHING, PAIR, TWOPAIR, TRIPLE, STRAIGHT, FULLHOUSE, FOUR, FIVE };// C의 enum은 이름을 모르는 int 상수라서 이름표를 따로 둔다(차례를 enum과 똑같이)static const char *NAME[] = {"NOTHING", "PAIR", "TWOPAIR", "TRIPLE", "STRAIGHT", "FULLHOUSE", "FOUR", "FIVE"};enum Hand classify(const int dice[5]) { int cnt[7] = {0}; // cnt[눈] = 그 눈이 나온 주사위 수 for (int k = 0; k < 5; k++) cnt[dice[k]]++; int most = 0, pairs = 0; bool triple = false; for (int v = 1; v <= 6; v++) { if (cnt[v] > most) most = cnt[v]; if (cnt[v] == 2) pairs++; if (cnt[v] == 3) triple = true; } if (most == 5) return FIVE; if (most == 4) return FOUR; if (triple && pairs == 1) return FULLHOUSE; // 다섯 눈이 모두 다르면 빠진 눈은 하나: 1이나 6이 빠졌을 때만 이어진다 if (most == 1 && (cnt[1] == 0 || cnt[6] == 0)) return STRAIGHT; if (triple) return TRIPLE; if (pairs == 2) return TWOPAIR; if (pairs == 1) return PAIR; return NOTHING;}int main(void) { int n; scanf("%d", &n); enum Hand best_hand = NOTHING; int best_sum = -1, winner = 0; for (int i = 1; i <= n; i++) { int dice[5], sum = 0; for (int k = 0; k < 5; k++) { scanf("%d", &dice[k]); sum += dice[k]; } enum Hand h = classify(dice); puts(NAME[h]); if (h > best_hand || (h == best_hand && sum > best_sum)) { best_hand = h; best_sum = sum; winner = i; } } printf("%d\n", winner); return 0;}