A car drove d kilometers and used l liters of fuel.
One line gives the integers d and l separated by one space.
Print the distance per liter of fuel, that is d ÷ l, with exactly two digits after the decimal point.
Write two digits even when the quotient is a whole number (25 ÷ 5 → 5.00).
The input always has d × 100 divisible by l. So the quotient ends within two decimal places, and no rounding is ever needed.
Input
1 ≤ d ≤ 100,000, 1 ≤ l ≤ 1,000, d × 100 is divisible by l
Output
d ÷ l on one line, exactly two digits after the decimal point
Example 1 · stdin
25 2
stdout
12.50
Example 2 · stdin
3 4
stdout
0.75
main.py
Error List00F8 ↓ · ⇧F8 ↑
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
The trap is integer division. In C, C++, Java and C#, d / l divides two ints and keeps only the quotient (3 / 4 → 0). Turn one side into a floating-point number first: (double)d / l in C, Java and C#, static_cast<double>(d) / l in C++. Converting after dividing, as in (double)(d / l), or just storing into a double, as in double km = d / l;, comes too late: the fraction is already gone and 3 4 gives 0.00. In Python 3, JS, TS and Craykon, / divides integers with decimals (Python's // keeps only the quotient). The second trap is the number of digits. Printing plainly gives 12.5 for Python print(d / l) and JS/TS console.log(d / l), and for a whole quotient JS prints 5 and Python 5.0. Fix two decimals with Python f"{x:.2f}", JS/TS x.toFixed(2), C printf("%.2f"), C++ std::fixed << std::setprecision(2), Java String.format("%.2f", x), C# x.ToString("F2") and Craykon Str.Fixed(x, 2).
import java.util.Locale;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int d = sc.nextInt(); int l = sc.nextInt(); // 한쪽을 double로 바꿔야 실수 나눗셈이 된다 double km = (double) d / l; System.out.println(String.format(Locale.ROOT, "%.2f", km)); }}
C#Main.cs · 15 lines
using System;using System.Globalization;class Program{ static void Main() { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int d = int.Parse(p[0]); int l = int.Parse(p[1]); // int끼리 나누면 몫만 남으니 한쪽을 double로 double km = (double)d / l; Console.WriteLine(km.ToString("F2", CultureInfo.InvariantCulture)); }}
C++main.cpp · 9 lines
#include <iomanip>#include <iostream>int main() { int d, l; std::cin >> d >> l; double km = static_cast<double>(d) / l; std::cout << std::fixed << std::setprecision(2) << km << "\n";}