{ }codelper
float divisionelse-if chainP0420

Bike path slope signs

A slope sign is put up at every section of a bike path.
The first line gives the number of sections n.
Each of the next n lines gives, for one section, the height it climbs h and its horizontal length d, separated by one space.
Both h and d are integers in centimeters.
The slope s of a section is h × 100 divided by d, computed exactly (in %).
Print "flat" if s is at most 3, "gentle" if s is greater than 3 and at most 7, "steep" if s is greater than 7 and at most 14, and "walk" if s is greater than 14.
A boundary value belongs to the lower step (s exactly 7 is "gentle", exactly 14 is "steep").
For each section, in input order, print its sign word on its own line.
On the last line, print the number of the section with the largest s.
Sections are numbered from 1 in input order.
If several sections share the largest s, print the smallest of their numbers.

Input
1 ≤ n ≤ 1,000, 1 ≤ d ≤ 100,000, 0 ≤ h ≤ d (all integers)
Output
n lines: one of flat, gentle, steep, walk per section.
Last line: the number of the section with the largest s
Example 1 · stdin
5
3 100
7 100
15 200
14 100
29 200
stdout
flat
gentle
steep
steep
walk
5
Example 2 · stdin
3
7 50
0 10
28 200
stdout
steep
flat
steep
1
main.py
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

This problem takes all 8 languages.
The slope formula is the same, but the order of operations and the division operator decide the answer at the boundaries.
Trap one: dividing before multiplying by 100.
h / d * 100 first makes 7 / 100 = 0.07 for 7 100.
0.07 cannot be stored exactly in binary, so it becomes the nearest double, and multiplying by 100 gives 7.000000000000001, which is greater than 7.
So 7 100 in the example prints "steep" and 14 100 prints "walk" (14 / 100 * 100 = 14.000000000000002, and 7 / 50 * 100 is 14.000000000000002 too).
All 8 languages compute with the same IEEE 754 double, so Python, JS, TS, Java, C#, C, C++ and Craykon go wrong in the same way.
With h * 100 / d, both h × 100 and d are exact integers and a single division gives the double nearest the true value, so a true value of 7 comes out as exactly 7.0.
Comparing the quotient directly, as in h / d <= 0.07, is also correct, because h / d and 0.07 are the same double, the one nearest 7/100.
To avoid real numbers altogether, compare integer products such as h * 100 <= 7 * d (the largest value is 10,000,000, well within a 32-bit int).
Trap two: integer division.
In C, C++, Java and C#, h * 100 / d between ints keeps only the quotient, so 15 200 (7.5%) in the example becomes 7 and prints "gentle".
Turn one side into a real number before dividing, as in h * 100.0 / d or (double) h * 100 / d.
Python's / divides into decimals even between integers, but // gives 7 the same way.
In JS, TS and Craykon / always divides into decimals; in Craykon you use Mathf.Quot when you want only the quotient.
Trap three: boundary values belong to the lower step.
Writing < makes 3 100 in the example "gentle" and 7 100 "steep".
Trap four: when several sections are steepest, print the earliest one.
Comparing with >= moves to the later section and prints 3 instead of 1 in example 2.
A multi-way condition is written elif in Python, else if in JS, TS, Java, C#, C and C++, and else if in Craykon too.
The JS and TS solutions chain the conditional operator ? :.

Pythonmain.py · 17 lines
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("flat")    elif slope <= 7:        print("gentle")    elif slope <= 14:        print("steep")    else:        print("walk")    if slope > best_slope:  # 같으면 앞 구간을 그대로 둔다        best, best_slope = i, slopeprint(best)
JavaScriptmain.js · 19 lines
const [n, ...v] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const sign = (slope) => (slope <= 3 ? "flat" : slope <= 7 ? "gentle" : slope <= 14 ? "steep" : "walk");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 lines
import { readFileSync } from "node:fs";const [n, ...v]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const sign = (slope: number): string =>  slope <= 3 ? "flat" : slope <= 7 ? "gentle" : slope <= 14 ? "steep" : "walk";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 lines
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 "flat";        if (slope <= 7) return "gentle";        if (slope <= 14) return "steep";        return "walk";    }     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 lines
using System;using System.Text; class Program{    static string Sign(double slope)    {        if (slope <= 3) return "flat";        if (slope <= 7) return "gentle";        if (slope <= 14) return "steep";        return "walk";    }     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 lines
#include <iostream>#include <string> std::string sign(double slope) {    if (slope <= 3) return "flat";    if (slope <= 7) return "gentle";    if (slope <= 14) return "steep";    return "walk";} 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 lines
#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("flat");        } else if (slope <= 7) {            puts("gentle");        } else if (slope <= 14) {            puts("steep");        } else {            puts("walk");        }        if (slope > best_slope) { /* 같으면 앞 구간을 그대로 둔다 */            best = i;            best_slope = slope;        }    }    printf("%d\n", best);    return 0;}
Craykonmain.cray · 34 lines
n = Console.ReadLine().ToNumber();가장 = 1;가장경사 = -1;for (i = 1; i <= n; i++){    구간 = Console.ReadNumbers();    h = 구간[0];    d = 구간[1];    // 100을 먼저 곱하고 나눈다: h / d * 100은 7 / 100 * 100 = 7.000000000000001이 된다    경사 = h * 100 / d;    if (경사 <= 3)    {        Console.WriteLine("flat");    }    else if (경사 <= 7)    {        Console.WriteLine("gentle");    }    else if (경사 <= 14)    {        Console.WriteLine("steep");    }    else    {        Console.WriteLine("walk");    }    // 같으면 앞 구간을 그대로 둔다    if (경사 > 가장경사)    {        가장 = i;        가장경사 = 경사;    }}Console.WriteLine(가장);