A phone plan includes 3 GB 500 MB of data per month.
Data used beyond the included amount is counted in blocks of 100 MB.
Even if the amount beyond does not fill a whole block, it counts as one block.
Each block costs 550 won extra.
If the amount used is equal to or less than the included amount, there are 0 blocks beyond and the extra charge is 0 won.
The units are 1 KB = 1,024 B, 1 MB = 1,024 KB, 1 GB = 1,024 MB.
Given n usage records for one month, find the total data used, the number of blocks beyond, and the extra charge.
Input
The first line contains the number of usage records n (1 ≤ n ≤ 300).
Each of the next n lines contains an integer a for the amount used and a unit u, separated by a single space (1 ≤ a ≤ 1,023).
u is one of B, KB, MB, GB.
Output
On the first line, print the total data used as an integer in B.
On the second line, print the number of blocks beyond.
On the third line, print the extra charge as an integer in won.
Print all three numbers as digits only, without commas.
Example 1 · stdin
4
3 GB
900 MB
512 KB
100 B
stdout
4165468260
5
2750
Example 2 · stdin
2
600 MB
3 GB
stdout
3850371072
1
550
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
Languages differ in how you make a constant, a name for a value that does not change.
Python has no constant syntax, so you write an uppercase name like KB = 1024 and agree not to change it. Assigning to it again raises no error.
In JavaScript, assigning to a const again throws a TypeError at run time, and TypeScript stops it earlier with a compile error.
Java uses static final, C# uses const, and C++ uses constexpr; in all three, assigning again is a compile error.
C uses a macro like #define KB 1024LL, and a macro substitutes text, not a value.
Craykon has no constants, so you keep a variable that you agree not to change.
Building one constant from another, as in MB = 1024 * KB, means the number 1,024 is written in only one place.
Trap one: overflow in a constant expression. 3 GB is 3,221,225,472 B, which does not fit in a 32-bit integer (maximum 2,147,483,647). In Java, static final long ALLOWANCE = 3 * 1024 * 1024 * 1024 + 500 * 1024 * 1024; multiplies ints before storing into the long and becomes -549,453,824 without any error. In example 1 the number of blocks comes out as 45 instead of 5. C#'s const and C++'s constexpr stop 3 * 1024 * 1024 * 1024 with a compile error, and C compiles it with only a warning (the result is undefined). Writing the first value as a 64-bit integer, like 1024L, fixes it. Python, JavaScript and Craykon do not overflow with integers of this size.
Trap two: a macro without parentheses. In C, with #define ALLOWANCE 3 * GB + 500 * MB, total - ALLOWANCE expands to total - 3 * GB + 500 * MB, which adds 500 MB instead of subtracting it. In example 1 the number of blocks comes out as 15.
Trap three: the units. In this problem 1 KB is 1,024 B, not 1,000 B. If the one constant line says 1000, the first line of example 1 comes out as 3900512100 instead of 4165468260.
Trap four: rounding up. In example 1 the amount beyond is 419,954,788 B, a little more than 4 blocks, so it is 5 blocks. Rounding down gives 4.
Trap five: an exact multiple. In example 2 the amount beyond is exactly 100 MB, so it is 1 block. Computing the round-up as quotient + 1 gives 2.
Pythonmain.py · 24 lines
import sys# 파이썬에는 상수 문법이 없다: 대문자 이름은 "바꾸지 말자"는 약속일 뿐이다KB = 1024MB = 1024 * KBGB = 1024 * MBALLOWANCE = 3 * GB + 500 * MB # 기본으로 주는 양BLOCK = 100 * MB # 넘은 양을 세는 묶음PRICE = 550 # 묶음 하나의 요금(원)UNIT = {"B": 1, "KB": KB, "MB": MB, "GB": GB}lines = sys.stdin.read().split("\n")n = int(lines[0])total = 0for i in range(1, n + 1): a, u = lines[i].split() total += int(a) * UNIT[u]blocks = 0if total > ALLOWANCE: blocks = (total - ALLOWANCE + BLOCK - 1) // BLOCK # 올림print(total)print(blocks)print(blocks * PRICE)
JavaScriptmain.js · 24 lines
// const는 다시 넣으면 실행 중에 TypeError가 난다const KB = 1024;const MB = 1024 * KB;const GB = 1024 * MB;const ALLOWANCE = 3 * GB + 500 * MB; // 기본으로 주는 양const BLOCK = 100 * MB; // 넘은 양을 세는 묶음const PRICE = 550; // 묶음 하나의 요금(원)const UNIT = { B: 1, KB: KB, MB: MB, GB: GB };const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);let total = 0;for (let i = 1; i <= n; i++) { const [a, u] = lines[i].trim().split(" "); total += Number(a) * UNIT[u];}let blocks = 0;if (total > ALLOWANCE) { blocks = Math.floor((total - ALLOWANCE + BLOCK - 1) / BLOCK); // 올림}console.log(String(total));console.log(String(blocks));console.log(String(blocks * PRICE));
TypeScriptmain.ts · 26 lines
import { readFileSync } from "node:fs";// const는 다시 넣으면 컴파일 오류다const KB: number = 1024;const MB: number = 1024 * KB;const GB: number = 1024 * MB;const ALLOWANCE: number = 3 * GB + 500 * MB; // 기본으로 주는 양const BLOCK: number = 100 * MB; // 넘은 양을 세는 묶음const PRICE: number = 550; // 묶음 하나의 요금(원)const UNIT: Record<string, number> = { B: 1, KB: KB, MB: MB, GB: GB };const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);let total: number = 0;for (let i = 1; i <= n; i++) { const [a, u] = lines[i].trim().split(" "); total += Number(a) * UNIT[u];}let blocks: number = 0;if (total > ALLOWANCE) { blocks = Math.floor((total - ALLOWANCE + BLOCK - 1) / BLOCK); // 올림}console.log(String(total));console.log(String(blocks));console.log(String(blocks * PRICE));
JavaMain.java · 40 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { // static final 상수. 1024L처럼 L을 붙여야 곱셈이 long으로 계산된다 static final long KB = 1024L; static final long MB = 1024 * KB; static final long GB = 1024 * MB; static final long ALLOWANCE = 3 * GB + 500 * MB; // 기본으로 주는 양 static final long BLOCK = 100 * MB; // 넘은 양을 세는 묶음 static final long PRICE = 550; // 묶음 하나의 요금(원) static long unit(String u) { switch (u) { case "KB": return KB; case "MB": return MB; case "GB": return GB; default: return 1; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); long total = 0; for (int i = 0; i < n; i++) { String[] parts = br.readLine().trim().split(" "); total += Long.parseLong(parts[0]) * unit(parts[1]); } long blocks = 0; if (total > ALLOWANCE) { blocks = (total - ALLOWANCE + BLOCK - 1) / BLOCK; // 올림 } System.out.println(total); System.out.println(blocks); System.out.println(blocks * PRICE); }}
C#Main.cs · 43 lines
using System;class Program{ // const 상수: 컴파일할 때 값이 정해진다 const long KB = 1024L; const long MB = 1024 * KB; const long GB = 1024 * MB; const long ALLOWANCE = 3 * GB + 500 * MB; // 기본으로 주는 양 const long BLOCK = 100 * MB; // 넘은 양을 세는 묶음 const long PRICE = 550; // 묶음 하나의 요금(원) static long Unit(string u) { switch (u) { case "KB": return KB; case "MB": return MB; case "GB": return GB; default: return 1; } } static void Main() { int n = int.Parse(Console.ReadLine().Trim()); long total = 0; for (int i = 0; i < n; i++) { string[] parts = Console.ReadLine().Trim().Split(' '); total += long.Parse(parts[0]) * Unit(parts[1]); } long blocks = 0; if (total > ALLOWANCE) { blocks = (total - ALLOWANCE + BLOCK - 1) / BLOCK; // 올림 } Console.WriteLine(total); Console.WriteLine(blocks); Console.WriteLine(blocks * PRICE); }}
C++main.cpp · 34 lines
#include <iostream>#include <string>using namespace std;// constexpr 상수: 컴파일할 때 계산되고, 넘치면 컴파일 오류다constexpr long long KB = 1024;constexpr long long MB = 1024 * KB;constexpr long long GB = 1024 * MB;constexpr long long ALLOWANCE = 3 * GB + 500 * MB; // 기본으로 주는 양constexpr long long BLOCK = 100 * MB; // 넘은 양을 세는 묶음constexpr long long PRICE = 550; // 묶음 하나의 요금(원)int main() { int n; cin >> n; long long total = 0; for (int i = 0; i < n; i++) { long long a; string u; cin >> a >> u; long long unit = 1; if (u == "KB") unit = KB; else if (u == "MB") unit = MB; else if (u == "GB") unit = GB; total += a * unit; } long long blocks = 0; if (total > ALLOWANCE) { blocks = (total - ALLOWANCE + BLOCK - 1) / BLOCK; // 올림 } cout << total << '\n' << blocks << '\n' << blocks * PRICE << '\n'; return 0;}
Cmain.c · 33 lines
#include <stdio.h>#include <string.h>/* #define은 글자를 그대로 바꿔 끼운다: 식은 꼭 괄호로 싼다 */#define KB 1024LL#define MB (1024 * KB)#define GB (1024 * MB)#define ALLOWANCE (3 * GB + 500 * MB) /* 기본으로 주는 양 */#define BLOCK (100 * MB) /* 넘은 양을 세는 묶음 */#define PRICE 550 /* 묶음 하나의 요금(원) */int main(void) { int n; if (scanf("%d", &n) != 1) return 1; long long total = 0; for (int i = 0; i < n; i++) { long long a; char u[4]; if (scanf("%lld %3s", &a, u) != 2) return 1; long long unit = 1; if (strcmp(u, "KB") == 0) unit = KB; else if (strcmp(u, "MB") == 0) unit = MB; else if (strcmp(u, "GB") == 0) unit = GB; total += a * unit; } long long blocks = 0; if (total > ALLOWANCE) { blocks = (total - ALLOWANCE + BLOCK - 1) / BLOCK; /* 올림 */ } printf("%lld\n%lld\n%lld\n", total, blocks, blocks * PRICE); return 0;}
Craykonmain.cray · 38 lines
// 크레이콘에는 상수가 없다: 바꾸지 않기로 약속한 변수로 둔다킬로 = 1024;메가 = 1024 * 킬로;기가 = 1024 * 메가;기본량 = 3 * 기가 + 500 * 메가; // 기본으로 주는 양묶음 = 100 * 메가; // 넘은 양을 세는 묶음묶음요금 = 550; // 묶음 하나의 요금(원)기록수 = Console.ReadLine().ToNumber();합 = 0;for (i = 0; i < 기록수; i++){ 칸들 = Console.ReadLine().Trim().Split(); 양 = 칸들[0].ToNumber(); 단위 = 1; if (칸들[1] == "KB") { 단위 = 킬로; } else if (칸들[1] == "MB") { 단위 = 메가; } else if (칸들[1] == "GB") { 단위 = 기가; } 합 += 양 * 단위;}묶음수 = 0;if (합 > 기본량){ 묶음수 = Mathf.Quot(합 - 기본량 + 묶음 - 1, 묶음); // 올림}Console.WriteLine(합);Console.WriteLine(묶음수);Console.WriteLine(묶음수 * 묶음요금);