Post

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

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

문제 링크


1. 아이디어

요금이 통화 하나마다 청구되므로 통화마다 두 요금제의 요금을 구해 각각 합한 뒤 더 싼 쪽을 출력하고, 합이 같으면 Y M과 그 요금을 함께 출력한다. 통화 하나의 시간을 $t$초라 하면 영식 요금은 $(\lfloor t / 30 \rfloor + 1) \times 10$원, 민식 요금은 $(\lfloor t / 60 \rfloor + 1) \times 15$원이다. 통화 시간이 청구 주기에 도달하는 순간 다음 구간으로 넘어가므로(영식은 29초까지 10원이고 30초부터 20원) 올림이 아니라 몫에 1을 더한다.


2. 코드

풀이 [Java][C++][Python]

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
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));

        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());

        int ySum = 0;
        int mSum = 0;

        while (n-- > 0) {
            int x = Integer.parseInt(st.nextToken());
            ySum += (x / 30 + 1) * 10;
            mSum += (x / 60 + 1) * 15;
        }

        if (ySum < mSum) {
            System.out.println("Y " + ySum);
        } else if (ySum > mSum) {
            System.out.println("M " + mSum);
        } else {
            System.out.println("Y M " + ySum);
        }
    }
}
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(0);
    cin.tie(0);

    int ySum = 0;
    int mSum = 0;

    int n;
    cin >> n;

    while (n--) {
        int x;
        cin >> x;

        ySum += (x / 30 + 1) * 10;
        mSum += (x / 60 + 1) * 15;
    }

    if (ySum < mSum) {
        cout << "Y " << ySum << '\n';
    } else if (ySum > mSum) {
        cout << "M " << mSum << '\n';
    } else {
        cout << "Y M " << ySum << '\n';
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import sys

input = sys.stdin.readline

input()
arr = list(map(int, input().split()))
y_sum = sum(x // 30 + 1 for x in arr) * 10
m_sum = sum(x // 60 + 1 for x in arr) * 15

if y_sum < m_sum:
    print("Y", y_sum)
elif y_sum > m_sum:
    print("M", m_sum)
else:
    print("Y M", y_sum)

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