In the Korean board game yutnori, a player throws the yut sticks at least once per turn.
If the result is yut or mo, the player throws once more; if it is do, gae or geol, the turn ends.
Results are written as numbers: do is 1, gae is 2, geol is 3, yut is 4 and mo is 5, and the piece moves that many spaces.
All the results one player threw over several turns are given in the order they were thrown.
The first line gives the number of throws n, and the second line gives the n results separated by single spaces.
The last result is one of 1, 2 and 3, so the last turn is given to its end.
For each turn, in order, print on its own line the sum of the results thrown in that turn (the number of spaces the piece moves in that turn).
Input
1 ≤ n ≤ 1,000. Each result is an integer from 1 to 5, and the last result is one of 1, 2 and 3
Output
One line per turn, each the number of spaces moved in that turn
Example 1 · stdin
6
4 2 3 5 4 1
stdout
6
3
10
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 is a loop that throws first and asks afterwards whether to throw again, which is exactly a do-while: do { t = a[i++]; total += t; } while (t >= 4);. Languages with do-while are C, C++, Java, C#, JS and TS. In C, C++, Java and C# the while (…) must be followed by a semicolon or it does not compile; JS and TS insert it automatically if you leave it out, but it is usually written. Python and Craykon have no do-while, so enter with while True: (Craykon while (true)) and build 'at least once' with if t < 4: break (Craykon break;) at the very end of the body. If you put the asking line before the adding line, the do, gae or geol that ended the turn is left out, and 4 2 gives 4 instead of 6. Writing it with an ordinary while means throwing the first time outside the loop, and it is easy to forget to add that first result. Mind the boundary too: yut (4) also throws again, so the test is t >= 4 (or t > 3); writing t > 4 or t == 5 ends the turn at yut.
Pythonmain.py · 13 lines
n = int(input())throws = list(map(int, input().split()))i = 0while i < n: total = 0 # 파이썬에는 do-while이 없다: while True로 들어가 끝에서 물어 "적어도 한 번" 던진다 while True: t = throws[i] i += 1 total += t if t < 4: # 도·개·걸이면 차례가 끝난다(더한 뒤에 묻는다) break print(total)
JavaScriptmain.js · 14 lines
const [n, ...throws] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out = [];let i = 0;while (i < n) { let total = 0; let t; // 한 번은 던지고, 윷(4)·모(5)가 나왔으면 다시 던진다 do { t = throws[i++]; total += t; } while (t >= 4); out.push(total);}console.log(out.join("\n"));
TypeScriptmain.ts · 14 lines
import { readFileSync } from "node:fs";const [n, ...throws]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out: number[] = [];let i = 0;while (i < n) { let total = 0; let t: number; do { t = throws[i++]; total += t; } while (t >= 4); out.push(total);}console.log(out.join("\n"));
JavaMain.java · 22 lines
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] throwsList = new int[n]; for (int k = 0; k < n; k++) throwsList[k] = sc.nextInt(); StringBuilder out = new StringBuilder(); int i = 0; while (i < n) { int total = 0; int t; do { t = throwsList[i++]; total += t; } while (t >= 4); // do-while은 세미콜론으로 끝난다 out.append(total).append('\n'); } System.out.print(out); }}
C#Main.cs · 26 lines
using System;using System.Linq;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine().Trim()); int[] throws = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); var sb = new StringBuilder(); int i = 0; while (i < n) { int total = 0; int t; do { t = throws[i++]; total += t; } while (t >= 4); sb.Append(total).Append('\n'); } Console.Write(sb); }}
C++main.cpp · 18 lines
#include <iostream>#include <vector>int main() { int n; std::cin >> n; std::vector<int> a(n); for (auto &x : a) std::cin >> x; int i = 0; while (i < n) { int total = 0, t; do { t = a[i++]; total += t; } while (t >= 4); std::cout << total << "\n"; }}
Cmain.c · 17 lines
#include <stdio.h>int main(void) { int n, a[1000]; scanf("%d", &n); for (int k = 0; k < n; k++) scanf("%d", &a[k]); int i = 0; while (i < n) { int total = 0, t; do { t = a[i++]; total += t; } while (t >= 4); /* 윷(4)·모(5)면 한 번 더 */ printf("%d\n", total); } return 0;}
Craykonmain.cray · 19 lines
n = Console.ReadLine().ToNumber();던짐 = Console.ReadNumbers();i = 0;while (i < n){ 합 = 0; // 크레이콘에는 do-while이 없다: 동안 (참)으로 들어가 끝에서 물어 그만 while (true) { t = 던짐[i]; i++; 합 = 합 + t; if (t < 4) { break; } } Console.WriteLine(합);}