EasyFunctionsNot started

Boards to cover a panel

A carpenter covers a fence panel by laying boards side by side across its length. Boards come in one width, and the last board may overhang, so the number needed is the length divided by the width, rounded up. Complete the function boards_needed(length: u32, width: u32) -> u32 so that it returns the smallest number of boards whose combined width is at least the panel length. The program reads the panel length and the board width, both in millimetres, and prints the function's result.

Input

Two lines: the panel length, then the board width, both positive integers in millimetres.

Output

One line: the number of boards.

Example 1

Input

200
15

Output

14

13 boards cover 195 mm, which is short; 14 cover 210 mm.

Example 2

Input

30
40

Output

1

One board is wider than the whole panel, so one is enough.

Constraints

  • 1 <= length, width <= 1000000

Hints

Hint 1 of 3

Integer division rounds down; you need the opposite. Ask how many full boards fit and whether anything is left over.

Hint 2 of 3

length / width, plus one more board if length % width is not zero.

Hint 3 of 3

The one-line form is (length + width - 1) / width, which rounds up without a branch and cannot overflow a u32 for these limits.

Solution

Show a reference solution and explanation
 Rust · reference solution
use std::io::{self, BufRead};

fn boards_needed(length: u32, width: u32) -> u32 {
    (length + width - 1) / width
}

fn main() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();
    let length: u32 = lines.next().unwrap().unwrap().trim().parse().unwrap();
    let width: u32 = lines.next().unwrap().unwrap().trim().parse().unwrap();
    println!("{}", boards_needed(length, width));
}

Why it works

The function is the point of the exercise: main only reads input and prints, while the calculation lives in boards_needed, where its parameter and return types are explicit and it could be tested on its own. A Rust function returns the value of its final expression, so no return keyword is needed. Rounding a division up with integers is a common need; adding width - 1 before dividing pushes any non-zero remainder over the next whole number while leaving exact multiples untouched. The largest intermediate value here is just under 2000000, well within u32.

Lesson for this exercise: Functions in Rust

Your program
use std::io::{self, BufRead};

fn boards_needed(length: u32, width: u32) -> u32 {
    // return the smallest number of boards whose widths add up to at least `length`
    0
}

fn main() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();
    let length: u32 = lines.next().unwrap().unwrap().trim().parse().unwrap();
    let width: u32 = lines.next().unwrap().unwrap().trim().parse().unwrap();
    println!("{}", boards_needed(length, width));
}
Run is not available for Rust in the browser yet. Write your program here, then download it and run it locally with rustc 1.94 against the examples above. The reference solution below was verified the same way.

Tests: 5 cases including the examples. Passing every test marks the exercise solved in this browser.

How this page was checked. Every program on it was run with rustc 1.94 at build time by the publishing checks, and the output shown is what it printed. Running Rust inside the browser is not available yet, so the Run button is absent rather than pretending; copy the code and run it with rustc 1.94 locally.