Write a function boxes(items, size, gap) that returns how many boxes are needed to pack the items.
A box has size slots, and gap slots of every box are left empty so nothing breaks.
So one box holds up to (size − gap) items.
The number of boxes needed is the smallest number of boxes that holds all the items. With 0 items it is 0.
If size is not given it counts as 12, and if gap is not given it counts as 0.
Gap is never given without size.
The first line gives the number of queries q, and each of the next q lines gives 1 to 3 integers separated by spaces.
The numbers are, in order, items, size and gap. So when there are two numbers, the second is size.
For each query print the number of boxes on its own line.
Try it with default parameter values (in languages without that syntax, several functions with the same name, or filling them in at the call).
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
How you write a default differs by language. Python def boxes(items, size=12, gap=0), JS/TS function boxes(items, size = 12, gap = 0), C# static long Boxes(long items, int size = 12, int gap = 0) and C++ long long boxes(long long items, int size = 12, int gap = 0) put = value after the parameter, and parameters with defaults go at the end. Java has no defaults, so you write boxes(items) and boxes(items, size) separately (overloads) and have them call the longer one; C and Craykon have neither defaults nor overloads, so the caller fills in 12 and 0 and passes all three (Craykon boxes(items, size, gap)). Arguments fill parameters from the front. So with two numbers the second is size, not gap. Declaring the parameters as (items, gap, size), or a Java overload passing its value to the wrong slot, gives wrong answers. In JS a missing argument is undefined and a default applies only to undefined, so without defaults the result is NaN. Python boxes(*nums) and JS boxes(...nums) spread a list into the arguments, but TS rejects spreading a number[] because the count is unknown (TS2556), so call by count. Rounding up is also a trap. items // k drops the leftover items, and quotient + 1 is one too many when it divides evenly or there are 0 items. In C, ceil(items / k) does nothing because integer division already rounded down, so use (items + k - 1) / k.
Pythonmain.py · 9 lines
def boxes(items, size=12, gap=0): per_box = size - gap # 상자 하나에 담는 물건 수 return (items + per_box - 1) // per_box # 올림 나눗셈(물건 0개면 0)q = int(input())for _ in range(q): nums = list(map(int, input().split())) # 수가 1~3개: 앞에서부터 items, size, gap에 들어가고 빠진 것은 기본값 print(boxes(*nums))
JavaScriptmain.js · 14 lines
// 빠진 인자는 undefined가 되고, 기본값은 undefined일 때만 쓰인다function boxes(items, size = 12, gap = 0) { const perBox = size - gap; return Math.ceil(items / perBox);}const lines = require("fs").readFileSync(0, "utf8").split("\n");const q = Number(lines[0]);const out = [];for (let i = 1; i <= q; i++) { const nums = lines[i].trim().split(/\s+/).map(Number); out.push(boxes(...nums));}console.log(out.join("\n"));
TypeScriptmain.ts · 18 lines
import { readFileSync } from "node:fs";function boxes(items: number, size: number = 12, gap: number = 0): number { const perBox = size - gap; return Math.ceil(items / perBox);}const lines: string[] = readFileSync(0, "utf8").split("\n");const q = Number(lines[0]);const out: number[] = [];for (let i = 1; i <= q; i++) { const nums = lines[i].trim().split(/\s+/).map(Number); // number[]를 ...로 펼쳐 넘기면 TS가 개수를 몰라 막는다(TS2556) — 개수대로 부른다 if (nums.length === 1) out.push(boxes(nums[0])); else if (nums.length === 2) out.push(boxes(nums[0], nums[1])); else out.push(boxes(nums[0], nums[1], nums[2]));}console.log(out.join("\n"));
JavaMain.java · 34 lines
import java.io.BufferedReader;import java.io.InputStreamReader;public class Main { // Java에는 기본값 매개변수가 없다 — 같은 이름의 메서드를 개수별로 두고(오버로드) 긴 쪽에 넘긴다 static long boxes(long items) { return boxes(items, 12); } static long boxes(long items, int size) { return boxes(items, size, 0); } static long boxes(long items, int size, int gap) { int perBox = size - gap; return (items + perBox - 1) / perBox; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int q = Integer.parseInt(br.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < q; i++) { String[] p = br.readLine().trim().split("\\s+"); long items = Long.parseLong(p[0]); long r; if (p.length == 1) r = boxes(items); else if (p.length == 2) r = boxes(items, Integer.parseInt(p[1])); else r = boxes(items, Integer.parseInt(p[1]), Integer.parseInt(p[2])); out.append(r).append('\n'); } System.out.print(out); }}
C#Main.cs · 31 lines
using System;using System.Text;class Program{ // 기본값은 컴파일할 때 정해지는 상수여야 하고, 기본값이 있는 매개변수는 뒤쪽에 둔다 static long Boxes(long items, int size = 12, int gap = 0) { int perBox = size - gap; return (items + perBox - 1) / perBox; } static void Main() { int q = int.Parse(Console.ReadLine().Trim()); var sb = new StringBuilder(); for (int i = 0; i < q; i++) { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); long items = long.Parse(p[0]); long r = p.Length switch { 1 => Boxes(items), 2 => Boxes(items, int.Parse(p[1])), _ => Boxes(items, int.Parse(p[1]), int.Parse(p[2])), }; sb.Append(r).Append('\n'); } Console.Write(sb); }}
C++main.cpp · 29 lines
#include <iostream>#include <sstream>#include <string>#include <vector>// 기본값은 선언에 한 번만 적고, 기본값이 있는 매개변수는 뒤쪽에 둔다long long boxes(long long items, int size = 12, int gap = 0) { int perBox = size - gap; return (items + perBox - 1) / perBox;}int main() { std::ios::sync_with_stdio(false); std::string line; std::getline(std::cin, line); int q = std::stoi(line); for (int i = 0; i < q; i++) { std::getline(std::cin, line); std::istringstream in(line); std::vector<long long> v; long long x; while (in >> x) v.push_back(x); long long r; if (v.size() == 1) r = boxes(v[0]); else if (v.size() == 2) r = boxes(v[0], (int)v[1]); else r = boxes(v[0], (int)v[1], (int)v[2]); std::cout << r << "\n"; }}
Cmain.c · 25 lines
#include <stdio.h>/* C에는 기본값 매개변수도 오버로드도 없다 — 부르는 쪽이 빠진 값을 채워 세 개를 모두 넘긴다 */#define DEFAULT_SIZE 12#define DEFAULT_GAP 0long boxes(long items, int size, int gap) { int per_box = size - gap; return (items + per_box - 1) / per_box;}int main(void) { int q; char line[128]; if (!fgets(line, sizeof line, stdin)) return 0; sscanf(line, "%d", &q); for (int i = 0; i < q; i++) { fgets(line, sizeof line, stdin); long items; int size = DEFAULT_SIZE, gap = DEFAULT_GAP; sscanf(line, "%ld %d %d", &items, &size, &gap); /* 읽지 못한 값은 기본값 그대로 */ printf("%ld\n", boxes(items, size, gap)); } return 0;}
Craykonmain.cray · 24 lines
// 크레이콘에는 기본값 매개변수가 없다 — 부르는 쪽이 빠진 값을 채워 세 개를 모두 넘긴다상자수(items, size, gap){ perBox = size - gap; return Mathf.Quot(items + perBox - 1, perBox); // 올림 나눗셈}q = Console.ReadLine().ToNumber();for (i = 0; i < q; i++){ p = Console.ReadLine().Trim().Split(" "); items = p[0].ToNumber(); size = 12; gap = 0; if (p.Count >= 2) { size = p[1].ToNumber(); } if (p.Count >= 3) { gap = p[2].ToNumber(); } Console.WriteLine(상자수(items, size, gap));}