An alarm system has a log of n records.
Each record lists three states at that moment, in this order: whether the alarm is armed, whether the door is open, and whether the window is open.
Each state is the lowercase word true or false.
The siren is off at the start, and after each record it is decided by the following rule.
If the alarm is not armed (false), the siren is off.
If the alarm is armed (true), the siren sounds when it was already sounding up to the previous record, or when at least one of the door and the window is open now.
If the alarm is armed but neither of those two cases holds, the siren is off.
So once the siren starts, it keeps sounding even if the door and the window are both closed, until the alarm is disarmed.
For each record, print true on its own line if the siren sounds after reading that record, and false otherwise.
On the last line print how many times the siren started sounding after being off.
Input
The first line gives n (1 ≤ n ≤ 1,000), and each of the next n lines gives three words, each true or false, separated by one space.
Output
n + 1 lines. The first n lines are lowercase true or false; the last line is the number of times the siren started sounding (0 if never)
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
One boolean (the siren) is recomputed on every line as armed && (siren || door || window). In all eight languages && (Python and) binds tighter than || (or). What differs is how to turn the word "true" into a boolean and how to print it back. Reading: Java has Boolean.parseBoolean(word), C# has bool.Parse(word), C++ can read straight into a bool after std::cin >> std::boolalpha, and Python, JavaScript, TypeScript, C and Craykon build it with a comparison such as word == "true" (in C, strcmp(word, "true") == 0, and bool needs <stdbool.h>). Writing: Java and JS/TS print true/false as is, Python and C# print True/False, C++ prints 1/0 without std::boolalpha, and C's printf has no format for bool. Craykon spells its booleans true and false, and here it picks the word with siren ? "true" : "false". Five traps: (1) Python bool("false") and JS Boolean("false") are true. Every non-empty string is truthy, so compare instead of converting. (2) Without the parentheses, armed && siren || door || window means (armed && siren) || door || window, so the siren sounds when the door opens even though the alarm is not armed. (3) If the previous siren value is left out of the expression, the siren stops the moment the door closes. (4) The output must be lowercase. Appending a bool in C# gives True, and C++ without std::boolalpha gives 1. (5) The count is not the number of lines where the siren sounds but the number of lines where it was off before and sounds now. Keep the previous value in its own variable before assigning the new one.
Pythonmain.py · 12 lines
n = int(input())siren = Falsecount = 0for _ in range(n): # bool("false")는 True다(빈 문자열만 False). 그래서 "true"와 같은지 비교해서 bool을 만든다 armed, door, window = (word == "true" for word in input().split()) was = siren siren = armed and (siren or door or window) if siren and not was: count += 1 print("true" if siren else "false") # print(siren)은 True/False로 찍힌다print(count)
JavaScriptmain.js · 15 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);let siren = false;let count = 0;const out = [];for (let i = 1; i <= n; i++) { // Boolean("false")는 true다(빈 문자열만 false). 그래서 "true"와 같은지 비교한다 const [armed, door, windowOpen] = lines[i].split(" ").map((word) => word === "true"); const was = siren; siren = armed && (siren || door || windowOpen); if (siren && !was) count++; out.push(String(siren)); // "true" 또는 "false"}out.push(count);console.log(out.join("\n"));
TypeScriptmain.ts · 19 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);let siren = false;let count = 0;const out: string[] = [];for (let i = 1; i <= n; i++) { // Boolean("false")는 true다(빈 문자열만 false). 그래서 "true"와 같은지 비교한다 const flags: boolean[] = lines[i].split(" ").map((word) => word === "true"); const armed = flags[0]; const door = flags[1]; const windowOpen = flags[2]; const was = siren; siren = armed && (siren || door || windowOpen); if (siren && !was) count++; out.push(String(siren)); // "true" 또는 "false"}out.push(String(count));console.log(out.join("\n"));
JavaMain.java · 25 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); boolean siren = false; int count = 0; StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { String[] words = in.readLine().trim().split(" "); boolean armed = Boolean.parseBoolean(words[0]); boolean door = Boolean.parseBoolean(words[1]); boolean window = Boolean.parseBoolean(words[2]); boolean was = siren; siren = armed && (siren || door || window); if (siren && !was) count++; out.append(siren).append('\n'); // boolean은 true/false로 붙는다 } out.append(count).append('\n'); System.out.print(out); }}
C#Main.cs · 27 lines
using System;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); bool siren = false; int count = 0; var sb = new StringBuilder(); for (int i = 0; i < n; i++) { string[] words = Console.ReadLine().Split(' '); bool armed = bool.Parse(words[0]); bool door = bool.Parse(words[1]); bool window = bool.Parse(words[2]); bool was = siren; siren = armed && (siren || door || window); if (siren && !was) count++; // bool을 그대로 붙이면 True/False(첫 글자가 대문자)가 되므로 직접 고른다 sb.Append(siren ? "true" : "false").Append('\n'); } sb.Append(count).Append('\n'); Console.Write(sb); }}
C++main.cpp · 20 lines
#include <iostream>int main() { int n; std::cin >> n; // boolalpha를 켜면 bool을 1/0이 아니라 true/false라는 낱말로 읽고 쓴다 std::cin >> std::boolalpha; std::cout << std::boolalpha; bool siren = false; int count = 0; for (int i = 0; i < n; i++) { bool armed, door, window; std::cin >> armed >> door >> window; bool was = siren; siren = armed && (siren || door || window); if (siren && !was) count++; std::cout << siren << '\n'; } std::cout << count << '\n';}
Cmain.c · 30 lines
#include <stdbool.h>#include <stdio.h>#include <string.h>/* 낱말 하나를 읽어 "true"인지 돌려준다 */static bool read_flag(void) { char word[8]; scanf("%7s", word); return strcmp(word, "true") == 0;}int main(void) { int n; scanf("%d", &n); bool siren = false; int count = 0; for (int i = 0; i < n; i++) { bool armed = read_flag(); bool door = read_flag(); bool window = read_flag(); bool was = siren; /* &&가 ||보다 먼저 묶이므로 괄호가 꼭 있어야 한다 */ siren = armed && (siren || door || window); if (siren && !was) count++; /* printf에는 bool을 찍는 서식이 없다(%d로 찍으면 1/0) */ puts(siren ? "true" : "false"); } printf("%d\n", count); return 0;}
Craykonmain.cray · 19 lines
n = Console.ReadLine().ToNumber();경보 = false;횟수 = 0;for (i = 0; i < n; i++){ 낱말 = Console.ReadLine().Split(" "); // 문자열 "false"는 거짓이 아니다. "true"와 같은지 비교해서 참·거짓을 만든다 켜짐 = 낱말[0] == "true"; 문 = 낱말[1] == "true"; 창문 = 낱말[2] == "true"; 전 = 경보; 경보 = 켜짐 && (경보 || 문 || 창문); if (경보 && !전) { 횟수++; } Console.WriteLine(경보 ? "true" : "false");}Console.WriteLine(횟수);