Check n school trip application forms.
One form consists of a student's name and eight items, each answered yes or no.
The eight items are, in order: guardian consent, swimming sign-up, health certificate handed in, hiking sign-up, crafts sign-up, cooking sign-up, bringing a packed lunch, and school meal sign-up.
A form must keep all four of the following rules.
Rule 1: guardian consent must be yes.
Rule 2: if swimming is signed up for, the health certificate must be handed in (without swimming, the certificate may or may not be handed in).
Rule 3: exactly one of hiking, crafts and cooking must be signed up for.
Rule 4: exactly one of packed lunch and school meal must be yes.
Print one line per form, in the order given.
If all four rules are kept, print the name, a single space and ok.
Otherwise print the name followed by the numbers of all broken rules, smallest first, each preceded by a single space.
On the last line print the number of forms that keep all four rules.
Input
The first line has the number of forms n. Each of the next n lines has a name and the eight answers, separated by single spaces. A name is 1 to 10 lowercase English letters (it may be yes, no or ok, and names may repeat), and each answer is yes or no. 1 ≤ n ≤ 1,000
Output
n lines, one per form, each name ok or name followed by the broken rule numbers; then one last line with the number of forms that passed
Example 1 · stdin
4
mina yes yes yes no yes no yes no
jun no yes no yes yes yes yes yes
sora yes no no no no no no yes
dan yes no yes yes no no no yes
stdout
mina ok
jun 1 2 3 4
sora 3
dan ok
2
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
yes and no are strings, so first turn them into true/false values kept in variables, then write the rules with &&, || and ! (Python: and, or, not). The conversion is tok == "yes" in Python, C#, C++ and Craykon, tok === "yes" in JS/TS, tok.equals("yes") in Java and strcmp(tok, "yes") == 0 in C. The type is called bool in Python, C# and C++, boolean in JS/TS and Java; C needs <stdbool.h> for bool; Craykon has true and false. Six traps: ① Using the string itself as a condition without converting it. In Python, JavaScript, TypeScript and Craykon only the empty string is false, so "no" counts as true, and there is no error. Java, C# and C++ (std::string) stop this with a compile error. ② In Java tok == "yes" asks whether the two are the same object, not whether the contents match, so it is always false for a yes read from input. ③ C's strcmp returns 0 when the strings are equal. Using strcmp(tok, "yes") directly as true/false swaps yes and no. ④ 'Swimming requires the certificate' is not swim && health. Written that way, sora, who did not sign up for swimming, breaks rule 2. Keeping the rule is !swim || health; breaking it is swim && !health. ⑤ Writing 'exactly one of three' as hike ^ craft ^ cook is also true for jun, who answered yes to all three (it is true whenever an odd number are true). You have to count the true values. In Python, JavaScript, C and C++ true adds as 1 and false as 0, so hike + craft + cook == 1 works. TypeScript, Java, C# and Craykon refuse to add booleans (a compile error; in Craykon a runtime error), so count by hand with if (a) { count += 1; } or use C# Count(f => f). ⑥ 'Exactly one of two' is not packed || meal. With that, jun, who said yes to both, passes rule 4. Ask whether the two booleans differ: packed != meal (!== in JS/TS).
Pythonmain.py · 32 lines
import sysdef main(): lines = sys.stdin.read().split("\n") n = int(lines[0]) out = [] passed = 0 for line in lines[1:n + 1]: parts = line.split() name = parts[0] # "no"도 빈 문자열이 아니라서 참으로 통한다 → 꼭 "yes"와 견줘 참·거짓으로 바꾼다 consent, swim, health, hike, craft, cook, packed, meal = (tok == "yes" for tok in parts[1:9]) broken = [] if not consent: broken.append(1) if swim and not health: # '수영이면 확인서'가 깨지는 경우는 이것 하나뿐이다 broken.append(2) if hike + craft + cook != 1: # 참은 1, 거짓은 0으로 더해진다 broken.append(3) if packed == meal: # 둘 중 정확히 하나 = 두 값이 서로 다르다 broken.append(4) if broken: out.append(name + " " + " ".join(map(str, broken))) else: out.append(name + " ok") passed += 1 out.append(str(passed)) print("\n".join(out))main()
JavaScriptmain.js · 23 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const out = [];let passed = 0;for (let i = 1; i <= n; i++) { const parts = lines[i].trim().split(" "); const name = parts[0]; // "no"도 빈 문자열이 아니라서 참으로 통한다 → 꼭 "yes"와 견줘 참·거짓으로 바꾼다 const [consent, swim, health, hike, craft, cook, packed, meal] = parts.slice(1, 9).map((tok) => tok === "yes"); const broken = []; if (!consent) broken.push(1); if (swim && !health) broken.push(2); // '수영이면 확인서'가 깨지는 경우는 이것 하나뿐이다 if (hike + craft + cook !== 1) broken.push(3); // 더하면 true는 1, false는 0이 된다 if (packed === meal) broken.push(4); // 둘 중 정확히 하나 = 두 값이 서로 다르다 if (broken.length > 0) { out.push(name + " " + broken.join(" ")); } else { out.push(name + " ok"); passed++; }}out.push(passed);console.log(out.join("\n"));
TypeScriptmain.ts · 32 lines
import { readFileSync } from "node:fs";// TypeScript는 boolean끼리 더하지 못하게 막는다 → 참인 것을 직접 센다function countTrue(...flags: boolean[]): number { return flags.filter((f) => f).length;}const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const out: string[] = [];let passed = 0;for (let i = 1; i <= n; i++) { const parts: string[] = lines[i].trim().split(" "); const name: string = parts[0]; // "no"도 빈 문자열이 아니라서 참으로 통한다 → 꼭 "yes"와 견줘 boolean으로 바꾼다 const [consent, swim, health, hike, craft, cook, packed, meal]: boolean[] = parts .slice(1, 9) .map((tok) => tok === "yes"); const broken: number[] = []; if (!consent) broken.push(1); if (swim && !health) broken.push(2); // '수영이면 확인서'가 깨지는 경우는 이것 하나뿐이다 if (countTrue(hike, craft, cook) !== 1) broken.push(3); if (packed === meal) broken.push(4); // 둘 중 정확히 하나 = 두 값이 서로 다르다 if (broken.length > 0) { out.push(name + " " + broken.join(" ")); } else { out.push(name + " ok"); passed++; }}out.push(String(passed));console.log(out.join("\n"));
JavaMain.java · 44 lines
import java.io.*;import java.util.*;public class Main { // Java의 boolean은 수가 아니라서 더할 수 없다 → 1이나 0으로 바꿔 센다 static int countTrue(boolean... flags) { int count = 0; for (boolean f : flags) { if (f) count++; } return count; } 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(); int passed = 0; for (int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); String name = st.nextToken(); boolean[] v = new boolean[8]; for (int j = 0; j < 8; j++) { v[j] = st.nextToken().equals("yes"); // ==는 같은 객체인지를 묻는다. 내용은 equals로 견준다 } boolean consent = v[0], swim = v[1], health = v[2]; boolean hike = v[3], craft = v[4], cook = v[5]; boolean packed = v[6], meal = v[7]; StringBuilder broken = new StringBuilder(); if (!consent) broken.append(" 1"); if (swim && !health) broken.append(" 2"); // '수영이면 확인서'가 깨지는 경우는 이것 하나뿐이다 if (countTrue(hike, craft, cook) != 1) broken.append(" 3"); if (packed == meal) broken.append(" 4"); // 둘 중 정확히 하나 = 두 값이 서로 다르다 if (broken.length() > 0) { sb.append(name).append(broken).append('\n'); } else { sb.append(name).append(" ok\n"); passed++; } } sb.append(passed).append('\n'); System.out.print(sb); }}
C#Main.cs · 41 lines
using System;using System.Collections.Generic;using System.Linq;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()!.Trim()); var sb = new StringBuilder(); int passed = 0; for (int i = 0; i < n; i++) { string[] parts = Console.ReadLine()!.Split(' ', StringSplitOptions.RemoveEmptyEntries); string name = parts[0]; // C#의 ==는 문자열의 내용을 견준다 bool[] v = parts.Skip(1).Take(8).Select(tok => tok == "yes").ToArray(); bool consent = v[0], swim = v[1], health = v[2]; bool hike = v[3], craft = v[4], cook = v[5]; bool packed = v[6], meal = v[7]; var broken = new List<int>(); if (!consent) broken.Add(1); if (swim && !health) broken.Add(2); // '수영이면 확인서'가 깨지는 경우는 이것 하나뿐이다 // bool은 수가 아니라서 더할 수 없다 → 참인 것을 센다 if (new[] { hike, craft, cook }.Count(f => f) != 1) broken.Add(3); if (packed == meal) broken.Add(4); // 둘 중 정확히 하나 = 두 값이 서로 다르다 if (broken.Count > 0) { sb.Append(name).Append(' ').Append(string.Join(" ", broken)).Append('\n'); } else { sb.Append(name).Append(" ok\n"); passed++; } } sb.Append(passed).Append('\n'); Console.Write(sb); }}
C++main.cpp · 38 lines
#include <iostream>#include <string>int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; int passed = 0; for (int i = 0; i < n; i++) { std::string name; std::cin >> name; bool v[8]; for (int j = 0; j < 8; j++) { std::string tok; std::cin >> tok; v[j] = tok == "yes"; // std::string의 ==는 내용을 견준다 } bool consent = v[0], swim = v[1], health = v[2]; bool hike = v[3], craft = v[4], cook = v[5]; bool packed = v[6], meal = v[7]; bool r1 = consent; bool r2 = !swim || health; // 수영을 하지 않으면 확인서는 묻지 않는다 bool r3 = hike + craft + cook == 1; // bool은 1 아니면 0이라 더해서 셀 수 있다 bool r4 = packed != meal; // 둘 중 정확히 하나 = 두 값이 서로 다르다 if (r1 && r2 && r3 && r4) { std::cout << name << " ok\n"; passed++; } else { std::cout << name; if (!r1) std::cout << " 1"; if (!r2) std::cout << " 2"; if (!r3) std::cout << " 3"; if (!r4) std::cout << " 4"; std::cout << '\n'; } } std::cout << passed << '\n';}
Cmain.c · 39 lines
#include <stdbool.h>#include <stdio.h>#include <string.h>int main(void) { int n; if (scanf("%d", &n) != 1) return 1; int passed = 0; for (int i = 0; i < n; i++) { char name[11]; bool v[8]; if (scanf("%10s", name) != 1) return 1; for (int j = 0; j < 8; j++) { char tok[4]; if (scanf("%3s", tok) != 1) return 1; v[j] = strcmp(tok, "yes") == 0; // strcmp는 같을 때 0을 돌려준다 } bool consent = v[0], swim = v[1], health = v[2]; bool hike = v[3], craft = v[4], cook = v[5]; bool packed = v[6], meal = v[7]; bool r1 = consent; bool r2 = !swim || health; // 수영을 하지 않으면 확인서는 묻지 않는다 bool r3 = hike + craft + cook == 1; // bool은 1 아니면 0이라 더해서 셀 수 있다 bool r4 = packed != meal; // 둘 중 정확히 하나 = 두 값이 서로 다르다 if (r1 && r2 && r3 && r4) { printf("%s ok\n", name); passed++; } else { printf("%s", name); if (!r1) printf(" 1"); if (!r2) printf(" 2"); if (!r3) printf(" 3"); if (!r4) printf(" 4"); printf("\n"); } } printf("%d\n", passed); return 0;}
Craykonmain.cray · 62 lines
// 참·거짓은 수가 아니라서 더할 수 없다 → 참인 것을 직접 센다참개수(a, b, c){ count = 0; if (a) { count += 1; } if (b) { count += 1; } if (c) { count += 1; } return count;}n = Console.ReadLine().ToNumber();passed = 0;for (i = 0; i < n; i++){ parts = Console.ReadLine().Split(); name = parts[0]; // 문자열을 "yes"와 견줘 참·거짓으로 바꾼다 consent = parts[1] == "yes"; swim = parts[2] == "yes"; health = parts[3] == "yes"; hike = parts[4] == "yes"; craft = parts[5] == "yes"; cook = parts[6] == "yes"; packed = parts[7] == "yes"; meal = parts[8] == "yes"; broken = ""; if (!consent) { broken += " 1"; } if (swim && !health) { broken += " 2"; // '수영이면 확인서'가 깨지는 경우는 이것 하나뿐이다 } if (참개수(hike, craft, cook) != 1) { broken += " 3"; } if (packed == meal) { broken += " 4"; // 둘 중 정확히 하나 = 두 값이 서로 다르다 } if (broken == "") { Console.WriteLine(name + " ok"); passed += 1; } else { Console.WriteLine(name + broken); }}Console.WriteLine(passed);