A ride holds at most 30 people, and it starts empty. Keep the capacity 30 as a constant, and count the people inside now, the total let in, and the total turned away in variables kept outside your functions. For each command call a function (one for in, one for out) that updates those variables.
- in k: k people queue up. Only the smaller of k and the free places (30 − people inside) get in; the rest are turned away. Print how many got in.
- out k: k people come out. Print how many are inside afterwards.
After all commands, print the total let in and the total turned away on one line, separated by one space.
Input
First line n (1 ≤ n ≤ 1000), then n lines of in k or out k. For in, 1 ≤ k ≤ 100; for out, 1 ≤ k ≤ the number inside at that moment
Output
One line per command, then a last line "let-in turned-away"
Example 1 · stdin
6
in 12
in 25
out 10
in 4
out 20
in 40
stdout
12
18
20
4
4
26
60 21
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
Constants are spelled differently in each language: Python just uses an uppercase name like CAPACITY = 30 (nothing stops you changing it; typing.Final is for checkers), JS/TS const, Java static final int, C# const int, C #define CAPACITY 30 (or const int, though in C that is not a constant expression such as an array size), C++ constexpr int. Craykon has no constants, so you keep a variable you promise not to change, capacity = 30;. Variables outside functions differ too: Java and C# cannot have variables outside a class, so they use static fields; C and C++ use file-scope globals; JS/TS use an outer let. In Python a function can read an outer variable, but the moment it assigns, as in inside += a, the name becomes local and you get UnboundLocalError, so write global inside first. Craykon treats a top-level variable as the same variable inside functions, so inside = inside + a; changes the outer one; but if a parameter has the same name, as in enter(inside, k), then inside in the function is the parameter and the outer one stays as it was. Two common traps: passing the count as a parameter and changing it inside the function only changes a copy (C, Java, C#, JS and Python all pass numbers by value), and creating the counter inside the loop (for (...) { let inside = 0; ... }) restarts it from 0 on every line.
Pythonmain.py · 27 lines
CAPACITY = 30 # 상수: 대문자 이름은 '바꾸지 말자'는 약속일 뿐, Python이 막아 주지는 않는다inside = 0 # 함수 밖(모듈)에 있는 변수 — 모든 함수가 같이 쓴다admitted = 0turned_away = 0def enter(k): global inside, admitted, turned_away # 없으면 inside += ... 에서 UnboundLocalError a = min(k, CAPACITY - inside) # a는 이 함수 안에서만 있는 지역 변수 inside += a admitted += a turned_away += k - a return adef leave(k): global inside inside -= k return insiden = int(input())for _ in range(n): cmd, k = input().split() print(enter(int(k)) if cmd == "in" else leave(int(k)))print(admitted, turned_away)
JavaScriptmain.js · 28 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const CAPACITY = 30; // const: 다시 넣으면 TypeErrorlet inside = 0; // 함수 밖의 let — 아래 함수들이 같이 쓴다let admitted = 0;let turnedAway = 0;function enter(k) { const a = Math.min(k, CAPACITY - inside); // 함수 안의 const는 이 함수에서만 보인다 inside += a; admitted += a; turnedAway += k - a; return a;}function leave(k) { inside -= k; return inside;}const n = Number(lines[0]);const out = [];for (let i = 1; i <= n; i++) { const [cmd, k] = lines[i].trim().split(" "); out.push(cmd === "in" ? enter(Number(k)) : leave(Number(k)));}out.push(`${admitted} ${turnedAway}`);console.log(out.join("\n"));
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException;public class Main { static final int CAPACITY = 30; // final: 한 번 넣으면 못 바꾼다 static int inside = 0; // static 필드 — 모든 static 메서드가 같이 쓴다 static int admitted = 0; static int turnedAway = 0; static int enter(int k) { int a = Math.min(k, CAPACITY - inside); // 지역 변수 inside += a; admitted += a; turnedAway += k - a; return a; } static int leave(int k) { inside -= k; return inside; } 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(); for (int i = 0; i < n; i++) { String[] parts = br.readLine().trim().split(" "); int k = Integer.parseInt(parts[1]); out.append(parts[0].equals("in") ? enter(k) : leave(k)).append('\n'); } out.append(admitted).append(' ').append(turnedAway).append('\n'); System.out.print(out); }}
C#Main.cs · 39 lines
using System;using System.Text;class Program{ const int Capacity = 30; // const: 컴파일할 때 값이 박힌다 static int inside = 0; // static 필드 — 클래스 안의 모든 메서드가 같이 쓴다 static int admitted = 0; static int turnedAway = 0; static int Enter(int k) { int a = Math.Min(k, Capacity - inside); inside += a; admitted += a; turnedAway += k - a; return a; } static int Leave(int k) { inside -= k; return inside; } static void Main() { int n = int.Parse(Console.ReadLine()); var out_ = new StringBuilder(); for (int i = 0; i < n; i++) { string[] parts = Console.ReadLine().Trim().Split(' '); int k = int.Parse(parts[1]); out_.Append(parts[0] == "in" ? Enter(k) : Leave(k)).Append('\n'); } out_.Append(admitted).Append(' ').Append(turnedAway).Append('\n'); Console.Write(out_); }}
C++main.cpp · 34 lines
#include <algorithm>#include <iostream>#include <string>constexpr int CAPACITY = 30; // constexpr: 컴파일할 때 정해지는 상수int inside = 0; // 전역 변수 — 아래 함수들이 같이 쓴다int admitted = 0;int turnedAway = 0;int enter(int k) { int a = std::min(k, CAPACITY - inside); inside += a; admitted += a; turnedAway += k - a; return a;}int leave(int k) { inside -= k; return inside;}int main() { int n; std::cin >> n; for (int i = 0; i < n; i++) { std::string cmd; int k; std::cin >> cmd >> k; std::cout << (cmd == "in" ? enter(k) : leave(k)) << '\n'; } std::cout << admitted << ' ' << turnedAway << '\n';}
Cmain.c · 34 lines
#include <stdio.h>#include <string.h>#define CAPACITY 30 /* 전처리기가 글자 그대로 30으로 바꿔 넣는다 */static int inside = 0; /* 파일 범위 변수: 이 파일의 모든 함수가 같이 쓴다 */static int admitted = 0;static int turned_away = 0;int enter(int k) { int room = CAPACITY - inside; int a = k < room ? k : room; inside += a; admitted += a; turned_away += k - a; return a;}int leave(int k) { inside -= k; return inside;}int main(void) { int n, k; char cmd[4]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%3s %d", cmd, &k); printf("%d\n", strcmp(cmd, "in") == 0 ? enter(k) : leave(k)); } printf("%d %d\n", admitted, turned_away); return 0;}
Craykonmain.cray · 37 lines
// 크레이콘에는 const가 없다: 바꾸지 않기로 약속한 변수로 둔다정원 = 30;안 = 0;들어간수 = 0;돌려보낸수 = 0;// 맨 위에 만든 변수는 함수 안에서도 같은 변수다(같이 쓴다)들어가기(k){ a = Mathf.Min(k, 정원 - 안); 안 = 안 + a; 들어간수 = 들어간수 + a; 돌려보낸수 = 돌려보낸수 + k - a; return a;}나가기(k){ 안 = 안 - k; return 안;}n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){ parts = Console.ReadLine().Split(); k = parts[1].ToNumber(); if (parts[0] == "in") { Console.WriteLine(들어가기(k)); } else { Console.WriteLine(나가기(k)); }}Console.WriteLine(들어간수 + " " + 돌려보낸수);