把菜谱的用量表按要做的人数调整。 第一行给出菜谱规定的份数 b 和要做的份数 t,用一个空格分开。 第二行给出材料数 n。 接下来 n 行,每行给出材料名、菜谱用量 a 和位数 d,用单个空格分开。 调整后的用量 x 是 a × t ÷ b 的精确值。 如果 d 为 1 或 2,把 x 四舍五入到小数点后第 d 位,并且小数点后正好写 d 位(d 为 2 时 2.1 写成 2.10)。 如果 d 为 0,把 x 四舍五入成整数,不写小数点。 如果 d 为 -1,把 x 四舍五入到最接近的 10 的倍数;d 为 -2 时四舍五入到最接近的 100 的倍数,不写小数点(d 为 -1 时 237.8 写成 240)。 四舍五入的结果为 0 时,按位数写成 0、0.0、0.00 这样。 为了让四舍五入没有歧义,不会给出 x 乘以 10^d 后小数部分在 0.4 到 0.6(含)之间的输入。 按输入顺序,每种材料输出一行:材料名和调整后的用量,用一个空格分开。
输入
1 ≤ b ≤ 100,1 ≤ t ≤ 100,1 ≤ n ≤ 1,000,材料名为 1~20 个英文小写字母(同一个名字可能再次出现),0.01 ≤ a ≤ 10,000,a 的小数点后写 0~2 位(例如 250、1.5、0.75),d 是 -2、-1、0、1、2 之一,没有 x × 10^d 的小数部分在 0.4~0.6 的输入
b, t = map(int, input().split())n = int(input())for _ in range(n): name, a, d = input().split() d = int(d) x = float(a) * t / b # /는 늘 실수 나눗셈이다 if d > 0: print(name, f"{x:.{d}f}") else: # 자리가 0 이하이면 10^(-d) 단위로: round(x / 단위)는 정수를 돌려준다 step = 10 ** -d print(name, round(x / step) * step)
JavaScriptmain.js · 16 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const [b, t] = lines[0].trim().split(/\s+/).map(Number);const n = Number(lines[1]);const out = [];for (let i = 0; i < n; i++) { const [name, a, ds] = lines[2 + i].trim().split(/\s+/); const d = Number(ds); const x = (Number(a) * t) / b; if (d > 0) { out.push(`${name} ${x.toFixed(d)}`); // toFixed는 0~100자리만 받는다 } else { const step = 10 ** -d; out.push(`${name} ${Math.round(x / step) * step}`); }}console.log(out.join("\n"));
TypeScriptmain.ts · 17 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const [b, t] = lines[0].trim().split(/\s+/).map(Number);const n = Number(lines[1]);const out: string[] = [];for (let i = 0; i < n; i++) { const [name, a, ds] = lines[2 + i].trim().split(/\s+/); const d = Number(ds); const x: number = (Number(a) * t) / b; if (d > 0) { out.push(`${name} ${x.toFixed(d)}`); // toFixed는 0~100자리만 받는다 } else { const step = 10 ** -d; out.push(`${name} ${Math.round(x / step) * step}`); }}console.log(out.join("\n"));
JavaMain.java · 27 行
import java.util.Locale;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int b = sc.nextInt(), t = sc.nextInt(); int n = sc.nextInt(); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { String name = sc.next(); double a = Double.parseDouble(sc.next()); int d = sc.nextInt(); double x = a * t / b; // a가 double이라 실수 나눗셈이 된다 out.append(name).append(' '); if (d > 0) { // Locale.ROOT: 지역 설정과 상관없이 소수점을 점으로 쓴다 out.append(String.format(Locale.ROOT, "%." + d + "f", x)); } else { long step = d == 0 ? 1 : d == -1 ? 10 : 100; out.append(Math.round(x / step) * step); } out.append('\n'); } System.out.print(out); }}
C#Main.cs · 34 行
using System;using System.Globalization;using System.Text;class Program{ static void Main() { var inv = CultureInfo.InvariantCulture; string[] bt = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int b = int.Parse(bt[0]), t = int.Parse(bt[1]); int n = int.Parse(Console.ReadLine()); var out_ = new StringBuilder(); for (int i = 0; i < n; i++) { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); double a = double.Parse(p[1], inv); int d = int.Parse(p[2]); double x = a * t / b; out_.Append(p[0]).Append(' '); if (d > 0) { out_.Append(x.ToString("F" + d, inv)); // "F2": 소수 두 자리 } else { long step = d == 0 ? 1 : d == -1 ? 10 : 100; out_.Append((long)Math.Round(x / step) * step); } out_.Append('\n'); } Console.Write(out_); }}
C++main.cpp · 23 行
#include <cmath>#include <iomanip>#include <iostream>#include <string>int main() { int b, t, n; std::cin >> b >> t >> n; for (int i = 0; i < n; i++) { std::string name; double a; int d; std::cin >> name >> a >> d; double x = a * t / b; std::cout << name << ' '; if (d > 0) { std::cout << std::fixed << std::setprecision(d) << x << '\n'; } else { long long step = d == 0 ? 1 : d == -1 ? 10 : 100; std::cout << std::llround(x / step) * step << '\n'; } }}
Cmain.c · 21 行
#include <math.h>#include <stdio.h>int main(void) { int b, t, n; scanf("%d %d %d", &b, &t, &n); for (int i = 0; i < n; i++) { char name[32]; double a; int d; scanf("%31s %lf %d", name, &a, &d); double x = a * t / b; /* a가 double이라 실수 나눗셈이 된다 */ if (d > 0) { printf("%s %.*f\n", name, d, x); /* 자리 수를 인수로 넘긴다 */ } else { long long step = d == 0 ? 1 : d == -1 ? 10 : 100; printf("%s %lld\n", name, llround(x / step) * step); } } return 0;}
Craykonmain.cray · 27 行
bt = 控制台.读数字();b = bt[0];t = bt[1];n = 控制台.读行().转数字();循环 (i = 0; i < n; i++){ p = 控制台.读行().分割(); a = p[1].转数字(); d = p[2].转数字(); x = a * t / b; // 크레이콘의 / 는 늘 소수까지 나눈다 값 = ""; 如果 (d > 0) { 값 = 字符.保留小数(x, d); } 否则 { // 수학.반올림의 자리는 0~10만 된다: 10, 100 단위는 나눠서 반올림하고 다시 곱한다 step = 1; 循环 (k = 0; k < 0 - d; k++) { step *= 10; } 값 = 数学.四舍五入(x / step) * step; } 控制台.输出(p[0] + " " + 값);}