You summarize the elevator log of a building with floors from basement 9 up to floor 60.
Above-ground floors are 1 to 60, and basement floors are negative: basement 1 is -1 and basement 9 is -9.
This building has no floor 0, so the floor right below floor 1 is basement 1.
The elevator starts at floor 1.
The input has one integer per line, giving the floors the elevator must go to in order.
A 0 marks the end of the log.
The 0 itself is not a floor, and any lines after it are ignored.
If the requested floor is the floor the elevator is on, nothing happens.
Otherwise the elevator moves to that floor, and this counts as one stop.
The distance of one move is the number of floor gaps passed: floor 1 to 3 is 2, floor 1 to -1 is 1, and floor 2 to -2 is 3.
Every move goes either up or down.
If a move goes in a different direction from the move right before it, count one change of direction (the first move is not counted, and an ignored request is not a move).
Print the number of stops, the total distance moved and the number of direction changes on one line, separated by single spaces.
If 0 comes first, print 0 0 0.
Input
0 to 10,000 lines before the 0, each a non-zero integer from -9 to 60; the 0 always appears; 0 to 10,000 lines after it, each an integer from -9 to 60 (may be 0)
Output
One line: number of stops, total distance, number of direction changes
Example 1 · stdin
5
5
-2
3
1
0
7
0
9
stdout
4 16 3
Example 2 · stdin
1
-1
1
-1
0
stdout
3 3 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
A loop that runs until an end marker (a sentinel) must keep the order 'read, ask whether it is the marker, handle it, read again'. C while (scanf("%d", &x) == 1 && x != 0), C++ while (std::cin >> x && x != 0), Java while ((x = sc.nextInt()) != 0) and C# while ((x = int.Parse(Console.ReadLine())) != 0) put reading and asking in one condition. Python loops with while True: and leaves with if x == 0: break after reading. JS/TS split the whole input into lines, loop with for (const line of lines) and break at 0. Craykon reads once before the loop and reads again at the very end inside while (x != 0). Handling first, as in do { read and handle } while (x != 0), treats the end marker 0 as a floor too, sending the elevator to the missing floor 0. Reading to the end of input and only skipping 0 is wrong as well: it also handles the lines left after the 0. Craykon Console.ReadLine() gives the string "0", and the string "0" is not equal to the number 0, so comparing with 0 without ToNumber() runs past the marker and stops on the null at the end of input. In Python, JS, TS, Java, C#, C and C++ a same-floor request can simply be skipped with continue, because the reading sits in the loop condition or at the top of the loop, so the next line still gets read. When the line that reads again sits at the very end of the loop, as in Craykon, continue skips that line too and the loop spins on the same value until it stops with the error 'Took too long'. So the Craykon solution puts the handling inside if (target != floor) and keeps the reading line outside that block. Get the distance with Python abs, JS/TS/Java Math.abs, C# Math.Abs, C abs (<stdlib.h>), C++ std::abs (<cstdlib>) or Craykon Mathf.Abs, and subtract 1 for the missing floor 0 when the two floors have different signs (one above ground, one below). The direction is compared with the move right before, so a same-floor request must not erase the remembered direction.
Pythonmain.py · 20 lines
floor = 1stops = distance = turns = 0last_dir = 0 # 1이면 올라감, -1이면 내려감, 0이면 아직 움직이지 않음while True: target = int(input()) if target == 0: # 끝 표시: 0 뒤의 줄은 읽지 않는다 break if target == floor: continue # 같은 층: 정차도 아니고 방향도 그대로 gap = abs(target - floor) if (floor > 0) != (target > 0): gap -= 1 # 지상과 지하 사이에는 0층이 없다 d = 1 if target > floor else -1 if last_dir != 0 and d != last_dir: turns += 1 stops += 1 distance += gap last_dir = d floor = targetprint(stops, distance, turns)
JavaScriptmain.js · 20 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");let floor = 1;let stops = 0;let distance = 0;let turns = 0;let lastDir = 0; // 1이면 올라감, -1이면 내려감, 0이면 아직 움직이지 않음for (const line of lines) { const target = Number(line); if (target === 0) break; // 끝 표시: 뒤의 줄은 보지 않는다 if (target === floor) continue; // 같은 층: 정차도 아니고 방향도 그대로 let gap = Math.abs(target - floor); if (floor > 0 !== target > 0) gap -= 1; // 지상과 지하 사이에는 0층이 없다 const d = target > floor ? 1 : -1; if (lastDir !== 0 && d !== lastDir) turns++; stops++; distance += gap; lastDir = d; floor = target;}console.log(`${stops} ${distance} ${turns}`);
TypeScriptmain.ts · 21 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");let floor = 1;let stops = 0;let distance = 0;let turns = 0;let lastDir = 0; // 1이면 올라감, -1이면 내려감, 0이면 아직 움직이지 않음for (const line of lines) { const target = Number(line); if (target === 0) break; // 끝 표시: 뒤의 줄은 보지 않는다 if (target === floor) continue; // 같은 층: 정차도 아니고 방향도 그대로 let gap = Math.abs(target - floor); if (floor > 0 !== target > 0) gap -= 1; // 지상과 지하 사이에는 0층이 없다 const d = target > floor ? 1 : -1; if (lastDir !== 0 && d !== lastDir) turns++; stops++; distance += gap; lastDir = d; floor = target;}console.log(`${stops} ${distance} ${turns}`);
JavaMain.java · 23 lines
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int floor = 1, stops = 0, distance = 0, turns = 0; int lastDir = 0; // 1이면 올라감, -1이면 내려감, 0이면 아직 움직이지 않음 int target; // 끝 표시 0이 나오면 멈춘다: 0 뒤의 줄은 읽지 않는다 while ((target = sc.nextInt()) != 0) { if (target == floor) continue; // 같은 층: 정차도 아니고 방향도 그대로 int gap = Math.abs(target - floor); if ((floor > 0) != (target > 0)) gap--; // 지상과 지하 사이에는 0층이 없다 int d = target > floor ? 1 : -1; if (lastDir != 0 && d != lastDir) turns++; stops++; distance += gap; lastDir = d; floor = target; } System.out.println(stops + " " + distance + " " + turns); }}
C#Main.cs · 25 lines
using System;class Program{ static void Main() { int floor = 1, stops = 0, distance = 0, turns = 0; int lastDir = 0; // 1이면 올라감, -1이면 내려감, 0이면 아직 움직이지 않음 int target; // 끝 표시 0이 나오면 멈춘다: 0 뒤의 줄은 읽지 않는다 while ((target = int.Parse(Console.ReadLine())) != 0) { if (target == floor) continue; // 같은 층: 정차도 아니고 방향도 그대로 int gap = Math.Abs(target - floor); if ((floor > 0) != (target > 0)) gap--; // 지상과 지하 사이에는 0층이 없다 int d = target > floor ? 1 : -1; if (lastDir != 0 && d != lastDir) turns++; stops++; distance += gap; lastDir = d; floor = target; } Console.WriteLine($"{stops} {distance} {turns}"); }}
C++main.cpp · 21 lines
#include <cstdlib>#include <iostream>int main() { int floor = 1, stops = 0, distance = 0, turns = 0; int lastDir = 0; // 1이면 올라감, -1이면 내려감, 0이면 아직 움직이지 않음 int target; // 끝 표시 0이 나오면 멈춘다: 0 뒤의 줄은 읽지 않는다 while (std::cin >> target && target != 0) { if (target == floor) continue; // 같은 층: 정차도 아니고 방향도 그대로 int gap = std::abs(target - floor); if ((floor > 0) != (target > 0)) gap--; // 지상과 지하 사이에는 0층이 없다 int d = target > floor ? 1 : -1; if (lastDir != 0 && d != lastDir) turns++; stops++; distance += gap; lastDir = d; floor = target; } std::cout << stops << ' ' << distance << ' ' << turns << '\n';}
Cmain.c · 22 lines
#include <stdio.h>#include <stdlib.h>int main(void) { int floor = 1, stops = 0, distance = 0, turns = 0; int last_dir = 0; /* 1이면 올라감, -1이면 내려감, 0이면 아직 움직이지 않음 */ int target; /* 끝 표시 0이 나오면 멈춘다: 0 뒤의 줄은 읽지 않는다 */ while (scanf("%d", &target) == 1 && target != 0) { if (target == floor) continue; /* 같은 층: 정차도 아니고 방향도 그대로 */ int gap = abs(target - floor); if ((floor > 0) != (target > 0)) gap--; /* 지상과 지하 사이에는 0층이 없다 */ int d = target > floor ? 1 : -1; if (last_dir != 0 && d != last_dir) turns++; stops++; distance += gap; last_dir = d; floor = target; } printf("%d %d %d\n", stops, distance, turns); return 0;}
Craykonmain.cray · 33 lines
floor = 1;stops = 0;distance = 0;turns = 0;lastDir = 0; // 1이면 올라감, -1이면 내려감, 0이면 아직 움직이지 않음target = Console.ReadLine().ToNumber();// 끝 표시 0이 나오면 멈춘다: 0 뒤의 줄은 읽지 않는다while (target != 0){ if (target != floor) { gap = Mathf.Abs(target - floor); if (floor > 0 != target > 0) { gap--; // 지상과 지하 사이에는 0층이 없다 } d = -1; if (target > floor) { d = 1; } if (lastDir != 0 && d != lastDir) { turns++; } stops++; distance += gap; lastDir = d; floor = target; } target = Console.ReadLine().ToNumber();}Console.WriteLine(stops + " " + distance + " " + turns);