Post

[BaekJoon] 1267번 - 핸드폰 요금 [Java][C++]

[BaekJoon] 1267번 - 핸드폰 요금 [Java][C++]

문제 링크


1. 문제 풀이


통신 요금은 통화 시간을 $x$, 청구 주기를 $t$, 청구 금액을 $m$ 이라고 했을 때, $(\dfrac{x}{t} + 1) \times m$ 으로 구할 수 있다.


2. 코드


1. 풀이 [Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int ys = 0;
        int ms = 0;

        int N = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            int x = Integer.parseInt(st.nextToken());

            ys += (x / 30 + 1) * 10;
            ms += (x / 60 + 1) * 15;
        }

        if (ys < ms) {
            System.out.println("Y " + ys);
        } else if (ys > ms) {
            System.out.println("M " + ms);
        } else {
            System.out.println("Y M " + ys);
        }
    }
}


2. 풀이 [C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int ys = 0;
    int ms = 0;

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        ys += (x / 30 + 1) * 10;
        ms += (x / 60 + 1) * 15;
    }

    if (ys < ms) {
        cout << "Y " << ys << '\n';
    } else if (ys > ms) {
        cout << "M " << ms << '\n';
    } else {
        cout << "Y M " << ys << '\n';
    }
}

This post is licensed under CC BY 4.0 by the author.