{ }codelper
不换行输出类型转换P0164

气温条带图

气象站在 d 天里每天测量 h 次气温。
气温以 0.1 度为单位记成整数(-35 表示 -3.5 度)。
第一行给出 d 和 h,接下来 d 行每行给出当天的 h 个气温,以空格分隔。
设所有记录中最低的值为 lo,最高的值为 hi。
气温 x 的格子编号是 (x − lo) × 9 ÷ (hi − lo + 1) 舍去小数部分得到的整数,在 0 到 8 之间。
格子编号 0~8 依次用字符 . : - = + * # % @ 表示。
每天输出一行:按测量顺序把当天 h 个格子字符连在一起输出,中间不加空格。
最后一行以度为单位、保留一位小数输出 lo 和 hi,以一个空格分隔(-35 写作 -3.5,0 写作 0.0,200 写作 20.0)。
因为气温都以 0.1 度为单位,这两个值恰好能用一位小数精确表示。

输入
1 ≤ d ≤ 100,1 ≤ h ≤ 48,-400 ≤ 气温 ≤ 500(整数)
输出
d 行格子字符(每行 h 个),最后一行 "lo hi"(以度为单位,一位小数)
样例 1 · stdin
2 5
-35 -10 42 118 97
12 60 155 203 140
stdout
..-*+
:=%@#
-3.5 20.3
main.py
输入(stdin)

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

$ 就绪

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

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

数值类型在两个地方发生变化。
① 格子编号:C・C++・Java・C# 中两个 int 相 / 只留下商,所以要先乘。
写成 (x - lo) / width * 9,由于 x - lo 总小于 width,结果全是 0,也就是全是 .。
Python 3・JS・TS・Craykon 的 / 得到实数,要用作下标就得转成整数:Python 用 //(直接用 / 会在 SCALE[2.0] 处 TypeError),JS・TS 用 Math.floor(不转的话 SCALE[2.5] 是 undefined),Craykon 用 数学.向下取整。
除数是 hi - lo + 1。
漏掉 + 1,最高值会落到不存在的 9 号格子;所有值相同时还会除以 0。
格子按全部记录的 lo・hi 来定,而不是每天各自的。
② 把 0.1 度的整数写成度:转成实数再除(lo / 10.0,Python・JS 写 lo / 10)最安全。
用商和余数拼出 "整数.一位" 遇到负数会出错:C・C++・Java・C# 中 -5 / 10 是 0(JS 的 Math.trunc(-5 / 10) 也输出为 0),-0.5 变成 0.5;Python 中 -5 // 10 是 -1、-5 % 10 是 5,得到 -1.5。
一位小数:Python f"{x:.1f}",JS・TS toFixed(1),C printf("%.1f"),C++ std::fixed << std::setprecision(1),Java String.format(Locale.ROOT, "%.1f", x),C# ToString("F1", CultureInfo.InvariantCulture),Craykon 字符.保留小数(x, 1)。
不换行地逐个字符输出:Python print(c, end=""),JS・TS process.stdout.write(c)(console.log 总会换行),Java System.out.print(调用很多次会慢,所以先攒进 PrintWriter),C# Console.Write(这里攒进 StreamWriter),C putchar,C++ std::cout << c,Craykon 控制台.接着输出。
每行末尾另外输出换行。
C 里不要把一行攒好后用 printf(row) 输出:字符 % 会被当成格式,%% 变成一个 %。
请用 fputs(row, stdout) 或 printf("%s", row)。

Pythonmain.py · 14 行
SCALE = ".:-=+*#%@"  # 낮은 칸부터 높은 칸까지 아홉 글자 d, h = map(int, input().split())days = [list(map(int, input().split())) for _ in range(d)]lo = min(min(day) for day in days)hi = max(max(day) for day in days)width = hi - lo + 1for day in days:    for x in day:        # // 로 나눠야 정수다. / 는 실수라서 SCALE[2.5]처럼 칸 번호로 쓸 수 없다        print(SCALE[(x - lo) * 9 // width], end="")    print()# 0.1도 단위 정수를 실수로 바꿔 10으로 나눈다: -5 → -0.5print(f"{lo / 10:.1f} {hi / 10:.1f}")
JavaScriptmain.js · 16 行
const SCALE = ".:-=+*#%@"; // 낮은 칸부터 높은 칸까지 아홉 글자const nums = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const [d, h] = nums;const temps = nums.slice(2);const lo = Math.min(...temps);const hi = Math.max(...temps);const width = hi - lo + 1;for (let i = 0; i < d; i++) {  for (let j = 0; j < h; j++) {    const x = temps[i * h + j];    // / 는 실수라서 Math.floor로 정수 칸 번호를 만든다(SCALE[2.5]는 undefined)    process.stdout.write(SCALE[Math.floor(((x - lo) * 9) / width)]); // 줄을 바꾸지 않고 쓴다  }  process.stdout.write("\n");}console.log(`${(lo / 10).toFixed(1)} ${(hi / 10).toFixed(1)}`);
TypeScriptmain.ts · 17 行
import { readFileSync } from "node:fs";const SCALE = ".:-=+*#%@"; // 낮은 칸부터 높은 칸까지 아홉 글자const nums: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const [d, h] = nums;const temps: number[] = nums.slice(2);const lo: number = Math.min(...temps);const hi: number = Math.max(...temps);const width: number = hi - lo + 1;for (let i = 0; i < d; i++) {  for (let j = 0; j < h; j++) {    const x: number = temps[i * h + j];    // / 는 실수라서 Math.floor로 정수 칸 번호를 만든다    process.stdout.write(SCALE[Math.floor(((x - lo) * 9) / width)]); // 줄을 바꾸지 않고 쓴다  }  process.stdout.write("\n");}console.log(`${(lo / 10).toFixed(1)} ${(hi / 10).toFixed(1)}`);
JavaMain.java · 36 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.Locale;import java.util.StringTokenizer; public class Main {    static final String SCALE = ".:-=+*#%@"; // 낮은 칸부터 높은 칸까지 아홉 글자     public static void main(String[] args) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        StringTokenizer st = new StringTokenizer(in.readLine());        int d = Integer.parseInt(st.nextToken()), h = Integer.parseInt(st.nextToken());        int[][] t = new int[d][h];        int lo = Integer.MAX_VALUE, hi = Integer.MIN_VALUE;        for (int i = 0; i < d; i++) {            st = new StringTokenizer(in.readLine());            for (int j = 0; j < h; j++) {                t[i][j] = Integer.parseInt(st.nextToken());                lo = Math.min(lo, t[i][j]);                hi = Math.max(hi, t[i][j]);            }        }        int width = hi - lo + 1;        PrintWriter out = new PrintWriter(System.out); // print를 여러 번 해도 한 번에 내보낸다        for (int[] day : t) {            // int끼리는 몫만 남는다: 곱하기를 먼저 해야 한다((x - lo) / width * 9는 늘 0)            for (int x : day) out.print(SCALE.charAt((x - lo) * 9 / width));            out.println();        }        // int를 double로 바꿔 나눈다: -5 / 10은 0이지만 -5 / 10.0은 -0.5        out.println(String.format(Locale.ROOT, "%.1f %.1f", lo / 10.0, hi / 10.0));        out.flush();    }}
C#Main.cs · 31 行
using System;using System.Globalization;using System.IO;using System.Linq; class Program{    const string Scale = ".:-=+*#%@"; // 낮은 칸부터 높은 칸까지 아홉 글자     static void Main()    {        int[] dh = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();        int d = dh[0];        var days = new int[d][];        for (int i = 0; i < d; i++)            days[i] = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();        int lo = days.Min(r => r.Min()), hi = days.Max(r => r.Max());        int width = hi - lo + 1;        var out_ = new StreamWriter(Console.OpenStandardOutput()); // Write를 여러 번 해도 한 번에 내보낸다        foreach (int[] day in days)        {            // int끼리는 몫만 남는다: 곱하기를 먼저            foreach (int x in day) out_.Write(Scale[(x - lo) * 9 / width]);            out_.Write('\n');        }        // int를 double로 바꿔 나눈다: -5 / 10은 0, -5 / 10.0은 -0.5        var inv = CultureInfo.InvariantCulture;        out_.Write((lo / 10.0).ToString("F1", inv) + " " + (hi / 10.0).ToString("F1", inv) + "\n");        out_.Flush();    }}
C++main.cpp · 28 行
#include <algorithm>#include <iomanip>#include <iostream>#include <string>#include <vector> int main() {    const std::string SCALE = ".:-=+*#%@"; // 낮은 칸부터 높은 칸까지 아홉 글자    std::ios::sync_with_stdio(false);    int d, h;    std::cin >> d >> h;    std::vector<std::vector<int>> t(d, std::vector<int>(h));    int lo = 1000, hi = -1000;    for (auto& day : t)        for (int& x : day) {            std::cin >> x;            lo = std::min(lo, x);            hi = std::max(hi, x);        }    int width = hi - lo + 1;    for (const auto& day : t) {        // int끼리는 몫만 남는다: 곱하기를 먼저. << 는 줄을 바꾸지 않는다        for (int x : day) std::cout << SCALE[(x - lo) * 9 / width];        std::cout << '\n';    }    // int를 double로 바꿔 나눈다: -5 / 10은 0이지만 -5 / 10.0은 -0.5    std::cout << std::fixed << std::setprecision(1) << lo / 10.0 << ' ' << hi / 10.0 << '\n';}
Cmain.c · 25 行
#include <stdio.h> static const char SCALE[] = ".:-=+*#%@"; /* 낮은 칸부터 높은 칸까지 아홉 글자 */static int t[100][48]; int main(void) {    int d, h;    scanf("%d %d", &d, &h);    int lo = 1000, hi = -1000;    for (int i = 0; i < d; i++)        for (int j = 0; j < h; j++) {            scanf("%d", &t[i][j]);            if (t[i][j] < lo) lo = t[i][j];            if (t[i][j] > hi) hi = t[i][j];        }    int width = hi - lo + 1;    for (int i = 0; i < d; i++) {        /* int끼리는 몫만 남는다: 곱하기를 먼저. putchar는 줄을 바꾸지 않는다 */        for (int j = 0; j < h; j++) putchar(SCALE[(t[i][j] - lo) * 9 / width]);        putchar('\n');    }    /* int를 double로 바꿔 나눈다: -5 / 10은 0이지만 -5 / 10.0은 -0.5 */    printf("%.1f %.1f\n", lo / 10.0, hi / 10.0);    return 0;}
Craykonmain.cray · 30 行
// 낮은 칸부터 높은 칸까지 아홉 글자눈금 = ".:-=+*#%@";첫줄 = 控制台.读数字();d = 첫줄[0];날들 = [];循环 (i = 0; i < d; i++){    날들.添加(控制台.读数字());}가장낮음 = 날들[0][0];가장높음 = 날들[0][0];遍历 (날 在 날들){    遍历 (x 在 날)    {        가장낮음 = 数学.较小值(가장낮음, x);        가장높음 = 数学.较大值(가장높음, x);    }}폭 = 가장높음 - 가장낮음 + 1;遍历 (날 在 날들){    遍历 (x 在 날)    {        // / 는 소수까지 나눈다: 칸 번호는 수학.내림으로 정수로        控制台.接着输出(눈금[数学.向下取整((x - 가장낮음) * 9 / 폭)]);    }    控制台.输出("");}控制台.输出(字符.保留小数(가장낮음 / 10, 1) + " " + 字符.保留小数(가장높음 / 10, 1));