Meeting room requests arrive as strings of the form "HH:MM-HH:MM" (start time-end time). The first line gives the number of requests n, and each of the next n lines gives one request. Check each request against the following six rules in order, starting from rule 1.
1. Form: exactly 11 characters, the 3rd and 9th characters are ':', the 6th character is '-', and the other eight characters are all digits (0-9).
2. Clock: in both times the hour (HH) is 00 to 23 and the minute (MM) is 00 to 59.
3. Quarter hours: in both times the minute is one of 00, 15, 30, 45.
4. Order: the start time is earlier than the end time.
5. Opening hours: the start time is 09:00 or later, and the end time is 18:00 or earlier.
6. Length: the booking is at most 3 hours (180 minutes).
For each request print the number of the first rule it breaks on its own line, or 0 if it keeps them all. On the last line print the number of requests that keep every rule. Split the program into small functions that return true or false, such as "is a digit", "has the right form" and "is a valid time", and a function that returns the first broken rule number.
Input
1 ≤ n ≤ 100. Each request is a string of 1 to 20 non-space characters (ASCII codes 33 to 126)
Output
n + 1 lines: one of 0 to 6 per request, then the number of requests that keep every rule
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
You build one big check out of small functions that return true or false. The boolean type is Python bool (True/False), JS/TS boolean, Java boolean, C# bool, C++ bool, C bool from stdbool.h (before that, int 0/1), Craykon true/false (참/거짓). Ask about the length first, so that s[2] is safe. &&, and and || stop as soon as the answer is known, so a wrong length ends the test before any character is read. Trap one: if you look at s[2] first, a short string stops Python with IndexError, Java with StringIndexOutOfBoundsException and C# with IndexOutOfRangeException, gives undefined in JS, and reads past the end of the string in C. Trap two: do not leave the digit test to a conversion function. Python int("+9") and int("-1") return 9 and -1, JS parseInt("1a") reads only the leading 1, and C sscanf("%d:%d-%d:%d") also accepts +9. Write a separate function that asks '0' <= ch <= '9' for each character. Craykon also writes it as a string comparison, ch >= "0" && ch <= "9". Trap three: a booking that ends at 18:00 and one of exactly 180 minutes are allowed, so use <=.
Pythonmain.py · 46 lines
def is_digit(ch): return "0" <= ch <= "9" # 아스키 숫자만(str.isdigit은 다른 숫자 글자도 참)def well_formed(s): """규칙 1: HH:MM-HH:MM 꼴인가. 길이부터 물어야 s[2]가 안전하다.""" if len(s) != 11 or s[2] != ":" or s[5] != "-" or s[8] != ":": return False return all(is_digit(s[i]) for i in (0, 1, 3, 4, 6, 7, 9, 10))def valid_clock(h, m): return 0 <= h <= 23 and 0 <= m <= 59def on_grid(m): return m % 15 == 0def first_broken(s): """처음 어긴 규칙 번호, 모두 지키면 0.""" if not well_formed(s): return 1 h1, m1, h2, m2 = int(s[0:2]), int(s[3:5]), int(s[6:8]), int(s[9:11]) if not (valid_clock(h1, m1) and valid_clock(h2, m2)): return 2 if not (on_grid(m1) and on_grid(m2)): return 3 start, end = h1 * 60 + m1, h2 * 60 + m2 if not start < end: return 4 if not (start >= 9 * 60 and end <= 18 * 60): return 5 if end - start > 180: return 6 return 0n = int(input())passed = 0for _ in range(n): r = first_broken(input().strip()) print(r) if r == 0: passed += 1print(passed)
JavaScriptmain.js · 31 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const isDigit = (ch) => ch >= "0" && ch <= "9";// 규칙 1: HH:MM-HH:MM 꼴인가function wellFormed(s) { if (s.length !== 11 || s[2] !== ":" || s[5] !== "-" || s[8] !== ":") return false; return [0, 1, 3, 4, 6, 7, 9, 10].every((i) => isDigit(s[i]));}const validClock = (h, m) => h >= 0 && h <= 23 && m >= 0 && m <= 59;const onGrid = (m) => m % 15 === 0;// 처음 어긴 규칙 번호, 모두 지키면 0function firstBroken(s) { if (!wellFormed(s)) return 1; const [h1, m1, h2, m2] = [0, 3, 6, 9].map((i) => Number(s.slice(i, i + 2))); if (!(validClock(h1, m1) && validClock(h2, m2))) return 2; if (!(onGrid(m1) && onGrid(m2))) return 3; const start = h1 * 60 + m1; const end = h2 * 60 + m2; if (!(start < end)) return 4; if (!(start >= 9 * 60 && end <= 18 * 60)) return 5; if (end - start > 180) return 6; return 0;}const n = Number(lines[0]);const out = lines.slice(1, n + 1).map((line) => firstBroken(line.trim()));out.push(out.filter((r) => r === 0).length);console.log(out.join("\n"));
TypeScriptmain.ts · 32 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const isDigit = (ch: string): boolean => ch >= "0" && ch <= "9";// 규칙 1: HH:MM-HH:MM 꼴인가function wellFormed(s: string): boolean { if (s.length !== 11 || s[2] !== ":" || s[5] !== "-" || s[8] !== ":") return false; return [0, 1, 3, 4, 6, 7, 9, 10].every((i) => isDigit(s[i]));}const validClock = (h: number, m: number): boolean => h >= 0 && h <= 23 && m >= 0 && m <= 59;const onGrid = (m: number): boolean => m % 15 === 0;// 처음 어긴 규칙 번호, 모두 지키면 0function firstBroken(s: string): number { if (!wellFormed(s)) return 1; const [h1, m1, h2, m2] = [0, 3, 6, 9].map((i) => Number(s.slice(i, i + 2))); if (!(validClock(h1, m1) && validClock(h2, m2))) return 2; if (!(onGrid(m1) && onGrid(m2))) return 3; const start = h1 * 60 + m1; const end = h2 * 60 + m2; if (!(start < end)) return 4; if (!(start >= 9 * 60 && end <= 18 * 60)) return 5; if (end - start > 180) return 6; return 0;}const n = Number(lines[0]);const out: number[] = lines.slice(1, n + 1).map((line) => firstBroken(line.trim()));out.push(out.filter((r) => r === 0).length);console.log(out.join("\n"));
JavaMain.java · 55 lines
import java.io.*;public class Main { static boolean isDigit(char ch) { return ch >= '0' && ch <= '9'; // Character.isDigit은 다른 나라 숫자도 참 } // 규칙 1: HH:MM-HH:MM 꼴인가. 길이부터 물어야 charAt(2)가 안전하다 static boolean wellFormed(String s) { if (s.length() != 11 || s.charAt(2) != ':' || s.charAt(5) != '-' || s.charAt(8) != ':') return false; for (int i : new int[] {0, 1, 3, 4, 6, 7, 9, 10}) { if (!isDigit(s.charAt(i))) return false; } return true; } static boolean validClock(int h, int m) { return h >= 0 && h <= 23 && m >= 0 && m <= 59; } static boolean onGrid(int m) { return m % 15 == 0; } static int two(String s, int i) { return (s.charAt(i) - '0') * 10 + (s.charAt(i + 1) - '0'); } // 처음 어긴 규칙 번호, 모두 지키면 0 static int firstBroken(String s) { if (!wellFormed(s)) return 1; int h1 = two(s, 0), m1 = two(s, 3), h2 = two(s, 6), m2 = two(s, 9); if (!(validClock(h1, m1) && validClock(h2, m2))) return 2; if (!(onGrid(m1) && onGrid(m2))) return 3; int start = h1 * 60 + m1, end = h2 * 60 + m2; if (!(start < end)) return 4; if (!(start >= 9 * 60 && end <= 18 * 60)) return 5; if (end - start > 180) return 6; return 0; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); StringBuilder out = new StringBuilder(); int passed = 0; for (int i = 0; i < n; i++) { int r = firstBroken(br.readLine().trim()); out.append(r).append('\n'); if (r == 0) passed++; } out.append(passed).append('\n'); System.out.print(out); }}
C#Main.cs · 49 lines
using System;using System.Linq;using System.Text;class Program{ static bool IsDigit(char ch) => ch >= '0' && ch <= '9'; // char.IsDigit은 다른 나라 숫자도 참 // 규칙 1: HH:MM-HH:MM 꼴인가. 길이부터 물어야 s[2]가 안전하다 static bool WellFormed(string s) { if (s.Length != 11 || s[2] != ':' || s[5] != '-' || s[8] != ':') return false; return new[] { 0, 1, 3, 4, 6, 7, 9, 10 }.All(i => IsDigit(s[i])); } static bool ValidClock(int h, int m) => h >= 0 && h <= 23 && m >= 0 && m <= 59; static bool OnGrid(int m) => m % 15 == 0; // 처음 어긴 규칙 번호, 모두 지키면 0 static int FirstBroken(string s) { if (!WellFormed(s)) return 1; int h1 = int.Parse(s.Substring(0, 2)), m1 = int.Parse(s.Substring(3, 2)); int h2 = int.Parse(s.Substring(6, 2)), m2 = int.Parse(s.Substring(9, 2)); if (!(ValidClock(h1, m1) && ValidClock(h2, m2))) return 2; if (!(OnGrid(m1) && OnGrid(m2))) return 3; int start = h1 * 60 + m1, end = h2 * 60 + m2; if (!(start < end)) return 4; if (!(start >= 9 * 60 && end <= 18 * 60)) return 5; if (end - start > 180) return 6; return 0; } static void Main() { int n = int.Parse(Console.ReadLine().Trim()); var sb = new StringBuilder(); int passed = 0; for (int i = 0; i < n; i++) { int r = FirstBroken(Console.ReadLine().Trim()); sb.Append(r).Append('\n'); if (r == 0) passed++; } sb.Append(passed).Append('\n'); Console.Write(sb); }}
C++main.cpp · 44 lines
#include <iostream>#include <string>bool is_digit(char ch) { return ch >= '0' && ch <= '9'; }// 규칙 1: HH:MM-HH:MM 꼴인가. 길이부터 물어야 s[2]가 안전하다bool well_formed(const std::string& s) { if (s.size() != 11 || s[2] != ':' || s[5] != '-' || s[8] != ':') return false; for (int i : {0, 1, 3, 4, 6, 7, 9, 10}) if (!is_digit(s[i])) return false; return true;}bool valid_clock(int h, int m) { return h >= 0 && h <= 23 && m >= 0 && m <= 59; }bool on_grid(int m) { return m % 15 == 0; }int two(const std::string& s, int i) { return (s[i] - '0') * 10 + (s[i + 1] - '0'); }// 처음 어긴 규칙 번호, 모두 지키면 0int first_broken(const std::string& s) { if (!well_formed(s)) return 1; int h1 = two(s, 0), m1 = two(s, 3), h2 = two(s, 6), m2 = two(s, 9); if (!(valid_clock(h1, m1) && valid_clock(h2, m2))) return 2; if (!(on_grid(m1) && on_grid(m2))) return 3; int start = h1 * 60 + m1, end = h2 * 60 + m2; if (!(start < end)) return 4; if (!(start >= 9 * 60 && end <= 18 * 60)) return 5; if (end - start > 180) return 6; return 0;}int main() { int n, passed = 0; std::cin >> n; for (int i = 0; i < n; i++) { std::string s; std::cin >> s; // 빈칸 없는 문자열 int r = first_broken(s); std::cout << r << "\n"; if (r == 0) passed++; } std::cout << passed << "\n";}
Cmain.c · 47 lines
#include <stdbool.h>#include <stdio.h>#include <string.h>static bool is_digit(char ch) { return ch >= '0' && ch <= '9'; }/* 규칙 1: HH:MM-HH:MM 꼴인가. 길이부터 물어야 s[2]가 안전하다 */static bool well_formed(const char *s) { if (strlen(s) != 11 || s[2] != ':' || s[5] != '-' || s[8] != ':') return false; const int at[] = {0, 1, 3, 4, 6, 7, 9, 10}; for (int i = 0; i < 8; i++) if (!is_digit(s[at[i]])) return false; return true;}static bool valid_clock(int h, int m) { return h >= 0 && h <= 23 && m >= 0 && m <= 59; }static bool on_grid(int m) { return m % 15 == 0; }static int two(const char *s, int i) { return (s[i] - '0') * 10 + (s[i + 1] - '0'); }/* 처음 어긴 규칙 번호, 모두 지키면 0 */static int first_broken(const char *s) { if (!well_formed(s)) return 1; int h1 = two(s, 0), m1 = two(s, 3), h2 = two(s, 6), m2 = two(s, 9); if (!(valid_clock(h1, m1) && valid_clock(h2, m2))) return 2; if (!(on_grid(m1) && on_grid(m2))) return 3; int start = h1 * 60 + m1, end = h2 * 60 + m2; if (!(start < end)) return 4; if (!(start >= 9 * 60 && end <= 18 * 60)) return 5; if (end - start > 180) return 6; return 0;}int main(void) { int n, passed = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { char s[32]; scanf("%31s", s); /* 빈칸 없는 문자열, 많아야 20글자 */ int r = first_broken(s); printf("%d\n", r); if (r == 0) passed++; } printf("%d\n", passed); return 0;}
Craykonmain.cray · 80 lines
숫자인가(ch){ return ch >= "0" && ch <= "9";}// 규칙 1: HH:MM-HH:MM 꼴인가. 길이부터 물어야 s[2]가 안전하다꼴이맞나(s){ if (s.Length != 11 || s[2] != ":" || s[5] != "-" || s[8] != ":") { return false; } foreach (i in [0, 1, 3, 4, 6, 7, 9, 10]) { if (!숫자인가(s[i])) { return false; } } return true;}시각인가(h, m){ return h >= 0 && h <= 23 && m >= 0 && m <= 59;}십오분단위인가(m){ return m % 15 == 0;}// 처음 어긴 규칙 번호, 모두 지키면 0처음어긴규칙(s){ if (!꼴이맞나(s)) { return 1; } h1 = s.Substring(0, 2).ToNumber(); m1 = s.Substring(3, 2).ToNumber(); h2 = s.Substring(6, 2).ToNumber(); m2 = s.Substring(9, 2).ToNumber(); if (!(시각인가(h1, m1) && 시각인가(h2, m2))) { return 2; } if (!(십오분단위인가(m1) && 십오분단위인가(m2))) { return 3; } start = h1 * 60 + m1; end = h2 * 60 + m2; if (!(start < end)) { return 4; } if (!(start >= 9 * 60 && end <= 18 * 60)) { return 5; } if (end - start > 180) { return 6; } return 0;}n = Console.ReadLine().ToNumber();passed = 0;for (i = 0; i < n; i++){ r = 처음어긴규칙(Console.ReadLine().Trim()); Console.WriteLine(r); if (r == 0) { passed++; }}Console.WriteLine(passed);