n people play a card-drawing game with one shared pile of cards. The pile has m cards, and each card has one integer from 0 to 9 written on it. A card with 0 is called a rest card. People take turns in order from 1 to n, one turn each (after n comes 1 again). Everyone's score starts at 0. If the pile has no cards at all when a turn starts, the game ends. On a person's turn, set this turn's sum to 0 and do the following. 1. Draw one card from the top of the pile. A drawn card is never used again. 2. If the drawn card is a rest card, do nothing and go to 4. 3. If the drawn card has the same number as the most recent non-zero card drawn before it in this turn, set this turn's sum to 0 and end the turn at once without doing 4. This holds even if rest cards lie between the two cards. If the number is not the same (this includes the first non-zero card of the turn), add the drawn number to this turn's sum. 4. If this turn has drawn fewer than k cards counting rest cards, this turn's sum is less than T, and the pile still has cards, go back to 1. If any of the three fails, the turn ends. So every turn draws at least one card. When a turn ends, add this turn's sum to that person's score, and it becomes the next person's turn. The first line gives n, k, T and m separated by single spaces. The second line gives the numbers of the m cards in order from the top of the pile, separated by single spaces. On the first line, print the number of turns played until the game ends. On the second line, print the scores of people 1 to n separated by single spaces.
Input
1 ≤ n ≤ 10, 1 ≤ k ≤ 10, 1 ≤ T ≤ 100, 1 ≤ m ≤ 5,000, each card is an integer from 0 to 9. All numbers in the input are plain digits without commas
Output
Two lines
Example 1 · stdin
2 3 10 13
4 0 0 6 6 2 0 5 0 5 3 9 1
stdout
5
21 8
Example 2 · stdin
1 4 20 7
5 0 5 7 7 0 3
stdout
3
3
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
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 · 8 languages
A turn has the shape 'draw one card first, then ask at the end whether to draw again', which is do-while. C, C++, Java, C#, JS and TS write do { … } while (drawn < k && turnSum < target && pos < m); and pass over a rest card with if (card == 0) continue; (=== in JS and TS). Inside a do-while, continue goes to the while (…) condition below, not to the top of the body. So after a rest card the k-card limit, T and the remaining cards are asked about just the same. When the same number comes again, break leaves at once without asking the condition. Trap one: Python and Craykon have no do-while, so they ask at the very end inside while True: (Craykon while (true)) with if not (…): break (Craykon if (!(…)) { break; }). If you use continue for a rest card there (Craykon spells it the same way), it jumps to the top of the body and skips the question at the end. In example 1, person 1 draws 4 0 0 and then 6 as well, giving 4 and 18 23. This solution wraps what happens for a non-rest card in if card != 0: (Craykon if (card != 0)), so the question at the end is always reached. Trap two: a rest card does not change 'the non-zero card drawn just before'. If a rest card records the previous card as 0, example 2's 5 0 5 is not seen as the same number and the output is 2 and 27. When continue comes before the line that records the previous card, this holds by itself. Trap three: if you set the sum to 0 on a repeated number and then use continue instead of break, the turn does not end and keeps drawing. In example 1, person 2 draws 2 after 6 6. Trap four: rest cards count toward the number of cards drawn. If you continue before counting, example 1 gives 4 and 27 14. Whether cards remain is asked when a turn starts and in step 4. If the pile runs out in the middle of a turn, the sum so far is added, and the game ends when the next turn would start (the last turn of example 2, 0 3).
Pythonmain.py · 32 lines
import sysdata = sys.stdin.read().split()n, k, target, m = map(int, data[:4])cards = list(map(int, data[4 : 4 + m]))scores = [0] * npos = 0turns = 0player = 0while pos < m: # 차례를 시작할 때 카드가 없으면 놀이가 끝난다 turns += 1 turn_sum = 0 drawn = 0 prev = -1 # 이번 차례에 바로 앞에 뽑은 0이 아닌 카드(아직 없음) # Python에는 do-while이 없다: while True로 들어가 끝에서 멈출지 묻는다. # 여기서 쉼 카드에 continue를 쓰면 맨 위로 가서 끝의 물음을 건너뛴다. 그래서 if로 감싼다. while True: card = cards[pos] pos += 1 drawn += 1 if card != 0: if card == prev: # 같은 수가 이어 나오면 이번 차례는 0점으로 바로 끝 turn_sum = 0 break turn_sum += card prev = card if not (drawn < k and turn_sum < target and pos < m): break scores[player] += turn_sum player = (player + 1) % nprint(turns)print(" ".join(map(str, scores)))
JavaScriptmain.js · 26 lines
const data = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const [n, k, target, m] = data;const cards = data.slice(4, 4 + m);const scores = new Array(n).fill(0);let pos = 0, turns = 0, player = 0;while (pos < m) { // 차례를 시작할 때 카드가 없으면 놀이가 끝난다 turns++; let turnSum = 0, drawn = 0, prev = -1; // prev: 바로 앞에 뽑은 0이 아닌 카드 do { // 적어도 한 장은 뽑는다 const card = cards[pos++]; drawn++; if (card === 0) continue; // do-while의 continue는 아래 while 조건으로 간다 if (card === prev) { // 같은 수가 이어 나오면 0점으로 바로 끝 turnSum = 0; break; } turnSum += card; prev = card; } while (drawn < k && turnSum < target && pos < m); scores[player] += turnSum; player = (player + 1) % n;}console.log(`${turns}\n${scores.join(" ")}`);
TypeScriptmain.ts · 28 lines
import { readFileSync } from "node:fs";const data: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const [n, k, target, m] = data;const cards: number[] = data.slice(4, 4 + m);const scores: number[] = new Array<number>(n).fill(0);let pos = 0, turns = 0, player = 0;while (pos < m) { // 차례를 시작할 때 카드가 없으면 놀이가 끝난다 turns++; let turnSum = 0, drawn = 0, prev = -1; // prev: 바로 앞에 뽑은 0이 아닌 카드 do { // 적어도 한 장은 뽑는다 const card: number = cards[pos++]; drawn++; if (card === 0) continue; // do-while의 continue는 아래 while 조건으로 간다 if (card === prev) { // 같은 수가 이어 나오면 0점으로 바로 끝 turnSum = 0; break; } turnSum += card; prev = card; } while (drawn < k && turnSum < target && pos < m); scores[player] += turnSum; player = (player + 1) % n;}console.log(`${turns}\n${scores.join(" ")}`);
JavaMain.java · 38 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); int n = Integer.parseInt(st.nextToken()), k = Integer.parseInt(st.nextToken()); int target = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()); st = new StringTokenizer(in.readLine()); int[] cards = new int[m]; for (int i = 0; i < m; i++) cards[i] = Integer.parseInt(st.nextToken()); int[] scores = new int[n]; int pos = 0, turns = 0, player = 0; while (pos < m) { // 차례를 시작할 때 카드가 없으면 놀이가 끝난다 turns++; int turnSum = 0, drawn = 0, prev = -1; // prev: 바로 앞에 뽑은 0이 아닌 카드 do { // 적어도 한 장은 뽑는다 int card = cards[pos++]; drawn++; if (card == 0) continue; // do-while의 continue는 아래 while 조건으로 간다 if (card == prev) { // 같은 수가 이어 나오면 0점으로 바로 끝 turnSum = 0; break; } turnSum += card; prev = card; } while (drawn < k && turnSum < target && pos < m); scores[player] += turnSum; player = (player + 1) % n; } StringBuilder out = new StringBuilder(); out.append(turns).append('\n'); for (int i = 0; i < n; i++) out.append(i > 0 ? " " : "").append(scores[i]); System.out.println(out); }}
C#Main.cs · 35 lines
using System;class Program{ static void Main() { string[] head = Console.ReadLine()!.Trim().Split(' '); int n = int.Parse(head[0]), k = int.Parse(head[1]), target = int.Parse(head[2]), m = int.Parse(head[3]); int[] cards = Array.ConvertAll(Console.ReadLine()!.Trim().Split(' '), int.Parse); int[] scores = new int[n]; int pos = 0, turns = 0, player = 0; while (pos < m) // 차례를 시작할 때 카드가 없으면 놀이가 끝난다 { turns++; int turnSum = 0, drawn = 0, prev = -1; // prev: 바로 앞에 뽑은 0이 아닌 카드 do // 적어도 한 장은 뽑는다 { int card = cards[pos++]; drawn++; if (card == 0) continue; // do-while의 continue는 아래 while 조건으로 간다 if (card == prev) // 같은 수가 이어 나오면 0점으로 바로 끝 { turnSum = 0; break; } turnSum += card; prev = card; } while (drawn < k && turnSum < target && pos < m); scores[player] += turnSum; player = (player + 1) % n; } Console.WriteLine(turns); Console.WriteLine(string.Join(" ", scores)); }}
C++main.cpp · 32 lines
#include <iostream>#include <vector>int main() { std::ios::sync_with_stdio(false); int n, k, target, m; std::cin >> n >> k >> target >> m; std::vector<int> cards(m); for (int& c : cards) std::cin >> c; std::vector<int> scores(n, 0); int pos = 0, turns = 0, player = 0; while (pos < m) { // 차례를 시작할 때 카드가 없으면 놀이가 끝난다 turns++; int turnSum = 0, drawn = 0, prev = -1; // prev: 바로 앞에 뽑은 0이 아닌 카드 do { // 적어도 한 장은 뽑는다 int card = cards[pos++]; drawn++; if (card == 0) continue; // do-while의 continue는 아래 while 조건으로 간다 if (card == prev) { // 같은 수가 이어 나오면 0점으로 바로 끝 turnSum = 0; break; } turnSum += card; prev = card; } while (drawn < k && turnSum < target && pos < m); scores[player] += turnSum; player = (player + 1) % n; } std::cout << turns << '\n'; for (int i = 0; i < n; i++) std::cout << (i ? " " : "") << scores[i]; std::cout << '\n';}
Cmain.c · 31 lines
#include <stdio.h>int main(void) { int n, k, target, m; scanf("%d %d %d %d", &n, &k, &target, &m); static int cards[5000]; for (int i = 0; i < m; i++) scanf("%d", &cards[i]); int scores[10] = {0}; int pos = 0, turns = 0, player = 0; while (pos < m) { /* 차례를 시작할 때 카드가 없으면 놀이가 끝난다 */ turns++; int turnSum = 0, drawn = 0, prev = -1; /* prev: 바로 앞에 뽑은 0이 아닌 카드 */ do { /* 적어도 한 장은 뽑는다 */ int card = cards[pos++]; drawn++; if (card == 0) continue; /* do-while의 continue는 아래 while 조건으로 간다 */ if (card == prev) { /* 같은 수가 이어 나오면 0점으로 바로 끝 */ turnSum = 0; break; } turnSum += card; prev = card; } while (drawn < k && turnSum < target && pos < m); scores[player] += turnSum; player = (player + 1) % n; } printf("%d\n", turns); for (int i = 0; i < n; i++) printf(i ? " %d" : "%d", scores[i]); putchar('\n'); return 0;}
Craykonmain.cray · 43 lines
머리 = Console.ReadLine().Trim().Split();n = 머리[0].ToNumber();k = 머리[1].ToNumber();목표 = 머리[2].ToNumber();m = 머리[3].ToNumber();카드들 = Console.ReadNumbers();점수 = NewList(n, 0);자리 = 0;차례수 = 0;사람 = 0;while (자리 < m){ 차례수++; 합 = 0; 뽑은수 = 0; 앞카드 = -1; // 이번 차례에 바로 앞에 뽑은 0이 아닌 카드(아직 없음) // 크레이콘에는 do-while이 없다: 동안 (참)으로 들어가 끝에서 멈출지 묻는다. // 쉼 카드에 건너뛰기를 쓰면 맨 위로 가서 끝의 물음을 건너뛰므로, 만약으로 감싼다. while (true) { 카드 = 카드들[자리]; 자리++; 뽑은수++; if (카드 != 0) { if (카드 == 앞카드) { 합 = 0; break; } 합 += 카드; 앞카드 = 카드; } if (!(뽑은수 < k && 합 < 목표 && 자리 < m)) { break; } } 점수[사람] += 합; 사람 = (사람 + 1) % n;}Console.WriteLine(차례수);Console.WriteLine(점수.Join(" "));