{ }codelper
浮点除法多重条件P0420

自行车道坡度指示牌

要在自行车道的每个路段立一块坡度指示牌。
第一行给出路段数 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
输入(stdin)

运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。

$ 就绪

请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。

其他语言怎么写?查看各语言题解 · 8 种语言题解

本题接受全部 8 种语言。
坡度的式子相同,但计算顺序和除法运算符会让边界上的答案不同。
陷阱一:先除再乘 100 的顺序。
h / d * 100 对 7 100 会先算出 7 / 100 = 0.07。
0.07 无法用二进制精确表示,只能存成最接近的 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++、Craykon 都会同样出错。
先乘 100 的 h * 100 / d 中,h × 100 和 d 都是精确的整数,一次除法会给出最接近真值的 double,所以真值为 7 时结果恰好是 7.0。
另外,像 h / d <= 0.07 这样直接拿商和 0.07 比较也是对的,因为 h / d 和 0.07 是同一个 double,即最接近 7/100 的那个。
若想完全不用实数,也可以用整数相乘来比较,例如 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 和 Craykon 的 / 总是除到小数,在 Craykon 中只想要商时另用 数学.商。
陷阱三:边界值属于较低的一档。
写成 < 的话,样例中的 3 100 变成 "gentle",7 100 变成 "steep"。
陷阱四:坡度最大的路段有多个时取靠前的编号。
比较写成 >= 会换成后面的路段,样例 2 输出 3 而不是 1。
多分支条件在 Python 中写 elif,JS、TS、Java、C#、C、C++ 写 else if,Craykon 写 否则 如果。
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;        가장경사 = 경사;    }}控制台.输出(가장);