{ }codelper
float divisionP0314

Free-throw success rate sheet

A basketball club's record sheet lists the free-throw records of n players. The first line gives n. Each of the next n lines gives, for one player, the number of shots made m and the number of shots taken t, separated by one space. A player's success rate is m divided by t, multiplied by 100. For each player, in the given order, print the success rate on its own line, rounded to one decimal place. Always write the first decimal digit even when it is 0 (50.0, 100.0), and do not add a percent sign. For a player who never shot (t is 0) the rate cannot be determined, so print just one minus sign - on that line.

Input
1 ≤ n ≤ 100, 0 ≤ m ≤ t ≤ 1000 (all integers).
No input makes the fractional part of the success rate times 10 exactly 0.5
Output
n lines.
Each line is the success rate to one decimal place, or -
Example 1 · stdin
4
1 2
2 3
0 0
7 7
stdout
50.0
66.7
-
100.0
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

What / does with two integers differs by language.
In C, C++, Java and C#, / between ints keeps only the quotient.
So m / t * 100 prints 0.0 for 2 3, because 2 / 3 becomes 0 first (1 2 in the example also gives 0.0).
The reordered m * 100 / t is still int by int, gives 66 and prints 66.0.
You must turn one side into a real number before dividing, as in (double) m.
Storing the result in a double variable is too late.
In Python 3, / divides into decimals even between integers, and // is the one that keeps only the quotient.
Writing m * 100 // t gives 66.0 here too.
JS and TS have a single number type, so / always divides into decimals, and so does Craykon's /.
Dividing by zero also differs.
Without asking whether t is 0, Python stops with ZeroDivisionError.
JS and TS raise no error: 0 / 0 becomes NaN and NaN is printed as is.
In C, C++, Java and C#, 0 / 0 after converting to a real number is not an error either but a 'not a number' value, and C prints nan (-nan in some environments).
In Craykon 0 / 0 is also a not-a-number value, and Str.Fixed stops when it receives it.
In every language, ask whether t is 0 before dividing.
Printing 0.0 there is wrong too.
The rate of a player who never shot is not 0 but undefined, so print -.
To fix one decimal place: Python f"{x:.1f}", JS/TS x.toFixed(1), C printf("%.1f"), C++ std::fixed << std::setprecision(1), Java String.format("%.1f", x), C# x.ToString("F1"), Craykon Str.Fixed(x, 1).
Printing without it gives 50 and 66.66666666666666 in JS.

Pythonmain.py · 8 lines
n = int(input())for _ in range(n):    m, t = map(int, input().split())    if t == 0:        print("-")   # 0으로 나누면 ZeroDivisionError — 나누기 전에 먼저 묻는다    else:        # Python 3의 /는 정수끼리도 소수까지 나눈다(//는 몫만)        print(f"{m / t * 100:.1f}")
JavaScriptmain.js · 9 lines
const [n, ...v] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out = [];for (let i = 0; i < n; i++) {  const m = v[2 * i];  const t = v[2 * i + 1];  // JS의 0 / 0은 오류가 아니라 NaN이다 — 나누기 전에 먼저 묻는다  out.push(t === 0 ? "-" : ((m / t) * 100).toFixed(1));}console.log(out.join("\n"));
TypeScriptmain.ts · 10 lines
import { readFileSync } from "node:fs";const [n, ...v]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out: string[] = [];for (let i = 0; i < n; i++) {  const m: number = v[2 * i];  const t: number = v[2 * i + 1];  // 0 / 0은 오류가 아니라 NaN이다 — 나누기 전에 먼저 묻는다  out.push(t === 0 ? "-" : ((m / t) * 100).toFixed(1));}console.log(out.join("\n"));
JavaMain.java · 22 lines
import java.util.Locale;import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        StringBuilder out = new StringBuilder();        for (int i = 0; i < n; i++) {            int m = sc.nextInt();            int t = sc.nextInt();            if (t == 0) {                out.append("-\n");            } else {                // 나누기 전에 한쪽을 double로 바꿔야 소수가 남는다                double rate = (double) m / t * 100;                out.append(String.format(Locale.ROOT, "%.1f", rate)).append('\n');            }        }        System.out.print(out);    }}
C#Main.cs · 29 lines
using System;using System.Globalization;using System.Text; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine());        var sb = new StringBuilder();        for (int i = 0; i < n; i++)        {            string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);            int m = int.Parse(p[0]);            int t = int.Parse(p[1]);            if (t == 0)            {                sb.Append("-\n");            }            else            {                // 나누기 전에 한쪽을 double로 바꿔야 소수가 남는다                double rate = (double)m / t * 100;                sb.Append(rate.ToString("F1", CultureInfo.InvariantCulture)).Append('\n');            }        }        Console.Write(sb);    }}
C++main.cpp · 19 lines
#include <iomanip>#include <iostream> int main() {    int n;    std::cin >> n;    std::cout << std::fixed << std::setprecision(1);    for (int i = 0; i < n; i++) {        int m, t;        std::cin >> m >> t;        if (t == 0) {            std::cout << "-\n";        } else {            // 나누기 전에 한쪽을 double로 바꿔야 소수가 남는다            double rate = static_cast<double>(m) / t * 100;            std::cout << rate << "\n";        }    }}
Cmain.c · 18 lines
#include <stdio.h> int main(void) {    int n;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        int m, t;        scanf("%d %d", &m, &t);        if (t == 0) {            puts("-");        } else {            /* 나누기 전에 한쪽을 double로 바꿔야 소수가 남는다 */            double rate = (double)m / t * 100;            printf("%.1f\n", rate);        }    }    return 0;}
Craykonmain.cray · 16 lines
n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){    기록 = Console.ReadNumbers();    m = 기록[0];    t = 기록[1];    if (t == 0)    {        Console.WriteLine("-");    }    else    {        // 크레이콘의 /는 늘 소수까지 나눈다        Console.WriteLine(Str.Fixed(m / t * 100, 1));    }}