자전거 길의 구간마다 경사 안내판을 세운다. 첫 줄에 구간 수 n이 주어진다. 다음 n줄에는 구간마다 올라가는 높이 h와 수평 거리 d가 빈칸 하나로 띄워 주어진다. h와 d는 모두 센티미터 단위의 정수이다. 구간의 경사 s는 h × 100을 d로 나눈 값을 정확하게 계산한 것이다(단위는 %). s가 3 이하이면 "평탄", 3보다 크고 7 이하이면 "완만", 7보다 크고 14 이하이면 "가파름", 14보다 크면 "끌고 가기"를 출력한다. 경계값은 아래 단계에 들어간다(s가 딱 7이면 "완만", 딱 14이면 "가파름"). 구간마다 입력 차례대로 한 줄에 하나씩 안내판 낱말을 출력하라. 마지막 줄에는 s가 가장 큰 구간의 번호를 출력한다. 구간 번호는 입력 차례대로 1부터 센다. s가 가장 큰 구간이 여럿이면 그중 가장 앞 번호를 출력한다.
입력
1 ≤ n ≤ 1,000, 1 ≤ d ≤ 100,000, 0 ≤ h ≤ d(모두 정수)
출력
n줄: 구간마다 flat, gentle, steep, walk 가운데 하나. 마지막 줄: s가 가장 큰 구간의 번호
예시 1 · stdin
5
3 100
7 100
15 200
14 100
29 200
stdout
평탄
완만
가파름
가파름
끌고 가기
5
예시 2 · stdin
3
7 50
0 10
28 200
stdout
가파름
평탄
가파름
1
main.py
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
이 문제는 8개 언어를 모두 받아요. 경사는 같은 식인데 계산하는 순서와 나눗셈 연산자에 따라 경계에서 답이 갈려요. 함정 하나: 나누고 나서 100을 곱하는 순서예요. h / d * 100은 7 100에서 7 / 100 = 0.07을 먼저 만들어요. 0.07은 2진수로 딱 맞게 담을 수 없어 가장 가까운 double에 담기고, 100을 곱하면 7.000000000000001이 되어 7보다 커져요. 그래서 예시의 7 100이 "steep", 14 100이 "walk"로 찍혀요(14 / 100 * 100 = 14.000000000000002, 7 / 50 * 100도 14.000000000000002). 8개 언어가 모두 같은 IEEE 754 double로 계산하므로 Python·JS·TS·Java·C#·C·C++·크레이콘에서 똑같이 틀려요. 100을 먼저 곱한 h * 100 / d는 h × 100과 d가 둘 다 정확한 정수이고 나눗셈 한 번은 참값에 가장 가까운 double을 내므로, 참값이 7이면 정확히 7.0이 나와요. 참고로 h / d <= 0.07처럼 나눈 값을 그대로 0.07과 비교하는 것도 맞아요. h / d와 0.07이 모두 7/100에 가장 가까운 같은 double이기 때문이에요. 실수를 아예 피하려면 h * 100 <= 7 * d처럼 정수끼리 곱해서 비교해도 돼요(가장 큰 값이 10,000,000이라 32비트 int 안이에요). 함정 둘: 정수 나눗셈이에요. C·C++·Java·C#에서 int끼리의 h * 100 / d는 몫만 남겨서 예시의 15 200(7.5%)이 7이 되어 "gentle"로 찍혀요. h * 100.0 / d나 (double) h * 100 / d처럼 나누기 전에 실수로 바꿔야 해요. Python의 /는 정수끼리도 소수까지 나누지만 //로 쓰면 똑같이 7이 돼요. JS·TS와 크레이콘의 /는 늘 소수까지 나누고, 크레이콘에서 몫만 원할 때는 수학.몫을 따로 써요. 함정 셋: 경계값은 아래 단계예요. <로 쓰면 예시의 3 100이 "gentle", 7 100이 "steep"이 돼요. 함정 넷: 가장 가파른 구간이 여럿이면 앞 번호예요. 비교를 >=로 쓰면 뒤 구간으로 바뀌어 예시 2에서 1 대신 3이 찍혀요. 여러 갈래 조건은 Python elif, JS·TS·Java·C#·C·C++ else if, 크레이콘 아니면 만약으로 써요. JS·TS 풀이는 조건 연산자 ? :를 이어 붙였어요.
Pythonmain.py · 17 줄
n = int(input())best, best_slope = 1, -1.0for i in range(1, n + 1): h, d = map(int, input().split()) # 100을 먼저 곱하고 나눈다: h / d * 100은 7 / 100 * 100 = 7.000000000000001이 된다 slope = h * 100 / d if slope <= 3: print("평탄") elif slope <= 7: print("완만") elif slope <= 14: print("가파름") else: print("끌고 가기") if slope > best_slope: # 같으면 앞 구간을 그대로 둔다 best, best_slope = i, slopeprint(best)
JavaScriptmain.js · 19 줄
const [n, ...v] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const sign = (slope) => (slope <= 3 ? "평탄" : slope <= 7 ? "완만" : slope <= 14 ? "가파름" : "끌고 가기");const out = [];let best = 1;let bestSlope = -1;for (let i = 1; i <= n; i++) { const h = v[2 * i - 2]; const d = v[2 * i - 1]; // 100을 먼저 곱하고 나눈다: h / d * 100은 7 / 100 * 100 = 7.000000000000001이 된다 const slope = (h * 100) / d; out.push(sign(slope)); if (slope > bestSlope) { // 같으면 앞 구간을 그대로 둔다 best = i; bestSlope = slope; }}out.push(String(best));console.log(out.join("\n"));
TypeScriptmain.ts · 21 줄
import { readFileSync } from "node:fs";const [n, ...v]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const sign = (slope: number): string => slope <= 3 ? "평탄" : slope <= 7 ? "완만" : slope <= 14 ? "가파름" : "끌고 가기";const out: string[] = [];let best: number = 1;let bestSlope: number = -1;for (let i = 1; i <= n; i++) { const h: number = v[2 * i - 2]; const d: number = v[2 * i - 1]; // 100을 먼저 곱하고 나눈다: h / d * 100은 7 / 100 * 100 = 7.000000000000001이 된다 const slope: number = (h * 100) / d; out.push(sign(slope)); if (slope > bestSlope) { // 같으면 앞 구간을 그대로 둔다 best = i; bestSlope = slope; }}out.push(String(best));console.log(out.join("\n"));
JavaMain.java · 36 줄
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { static String sign(double slope) { if (slope <= 3) return "평탄"; if (slope <= 7) return "완만"; if (slope <= 14) return "가파름"; return "끌고 가기"; } 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 best = 1; double bestSlope = -1; for (int i = 1; i <= n; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); int h = Integer.parseInt(st.nextToken()); int d = Integer.parseInt(st.nextToken()); // int끼리 나누면 몫만 남으므로 double로 바꾼 뒤 나눈다. // 100을 먼저 곱한다: h / d * 100은 7 / 100 * 100 = 7.000000000000001이 된다 double slope = h * 100.0 / d; out.append(sign(slope)).append('\n'); if (slope > bestSlope) { // 같으면 앞 구간을 그대로 둔다 best = i; bestSlope = slope; } } out.append(best).append('\n'); System.out.print(out); }}
C#Main.cs · 38 줄
using System;using System.Text;class Program{ static string Sign(double slope) { if (slope <= 3) return "평탄"; if (slope <= 7) return "완만"; if (slope <= 14) return "가파름"; return "끌고 가기"; } static void Main() { int n = int.Parse(Console.ReadLine()); var out_ = new StringBuilder(); int best = 1; double bestSlope = -1; for (int i = 1; i <= n; i++) { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int h = int.Parse(p[0]); int d = int.Parse(p[1]); // int끼리 나누면 몫만 남으므로 double로 바꾼 뒤 나눈다. // 100을 먼저 곱한다: h / d * 100은 7 / 100 * 100 = 7.000000000000001이 된다 double slope = h * 100.0 / d; out_.Append(Sign(slope)).Append('\n'); if (slope > bestSlope) // 같으면 앞 구간을 그대로 둔다 { best = i; bestSlope = slope; } } out_.Append(best).Append('\n'); Console.Write(out_); }}
C++main.cpp · 30 줄
#include <iostream>#include <string>std::string sign(double slope) { if (slope <= 3) return "평탄"; if (slope <= 7) return "완만"; if (slope <= 14) return "가파름"; return "끌고 가기";}int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; int best = 1; double bestSlope = -1; for (int i = 1; i <= n; i++) { int h, d; std::cin >> h >> d; // int끼리 나누면 몫만 남으므로 double로 바꾼 뒤 나눈다. // 100을 먼저 곱한다: h / d * 100은 7 / 100 * 100 = 7.000000000000001이 된다 double slope = h * 100.0 / d; std::cout << sign(slope) << '\n'; if (slope > bestSlope) { // 같으면 앞 구간을 그대로 둔다 best = i; bestSlope = slope; } } std::cout << best << '\n';}
Cmain.c · 30 줄
#include <stdio.h>int main(void) { int n; scanf("%d", &n); int best = 1; double best_slope = -1; for (int i = 1; i <= n; i++) { int h, d; scanf("%d %d", &h, &d); /* int끼리 나누면 몫만 남으므로 double로 바꾼 뒤 나눈다. 100을 먼저 곱한다: h / d * 100은 7 / 100 * 100 = 7.000000000000001이 된다 */ double slope = h * 100.0 / d; if (slope <= 3) { puts("평탄"); } else if (slope <= 7) { puts("완만"); } else if (slope <= 14) { puts("가파름"); } else { puts("끌고 가기"); } if (slope > best_slope) { /* 같으면 앞 구간을 그대로 둔다 */ best = i; best_slope = slope; } } printf("%d\n", best); return 0;}
Craykonmain.cray · 34 줄
n = 콘솔.줄읽기().수로변환();가장 = 1;가장경사 = -1;반복 (i = 1; i <= n; i++){ 구간 = 콘솔.수들읽기(); h = 구간[0]; d = 구간[1]; // 100을 먼저 곱하고 나눈다: h / d * 100은 7 / 100 * 100 = 7.000000000000001이 된다 경사 = h * 100 / d; 만약 (경사 <= 3) { 콘솔.쓰기("평탄"); } 아니면 만약 (경사 <= 7) { 콘솔.쓰기("완만"); } 아니면 만약 (경사 <= 14) { 콘솔.쓰기("가파름"); } 아니면 { 콘솔.쓰기("끌고 가기"); } // 같으면 앞 구간을 그대로 둔다 만약 (경사 > 가장경사) { 가장 = i; 가장경사 = 경사; }}콘솔.쓰기(가장);