一轮就是“先掷,掷完再问要不要再掷”的循环,正好是 do-while:do { t = a[i++]; total += t; } while (t >= 4);。 有 do-while 的语言是 C、C++、Java、C#、JS、TS。 C、C++、Java、C# 的 while (…) 后面没有分号就无法编译;JS、TS 省略时会自动补上,但通常还是写上。 Python 和 Craykon 没有 do-while,用 while True:(Craykon 当 (真))进入,在循环体最后用 if t < 4: break(Craykon 跳出;)实现“至少一次”。 如果把判断的那一行放在相加的那一行前面,结束这一轮的刀、开、杰就会被漏掉,4 2 得到 4 而不是 6。 用普通 while 写时,第一次要在循环外面掷,很容易忘记把第一次的结果加上。 还要注意边界:尤(4)也要再掷,所以条件是 t >= 4(或 t > 3);写成 t > 4 或 t == 5,掷出尤时这一轮就结束了。
Pythonmain.py · 13 行
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 行
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 行
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 行
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 行
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 行
#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 行
#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 行
n = 控制台.读行().转数字();던짐 = 控制台.读数字();i = 0;当 (i < n){ 합 = 0; // 크레이콘에는 do-while이 없다: 동안 (참)으로 들어가 끝에서 물어 그만 当 (真) { t = 던짐[i]; i++; 합 = 합 + t; 如果 (t < 4) { 跳出; } } 控制台.输出(합);}