Algorithm

[BOJ] 1267번 : 핸드폰 요금

rudgh99_algo 2024. 8. 12. 01:44

1. problem :

https://www.acmicpc.net/problem/1267

 

2. solution 1 :

#include <bits/stdc++.h> 
using namespace std;

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int N;
	cin >> N;
	int young = 0;
	int minS = 0;
	while (N--) {
		int x;
		cin >> x; 
		int young_r = x / 30;
		int min_r = x / 60;
		young += (young_r + 1)* 10; 
		minS += (min_r + 1) * 15; 
	}
	if (young < minS) {
		cout << 'Y' << ' '; 
		cout << young << '\n';
	}
	else if (minS < young) {
		cout << 'M' << ' '; 
		cout << minS << '\n';
	}
	else {
		cout << 'Y' << ' ';
		cout << 'M' << ' ';
		cout << young << '\n';
	}
	return 0;
}

29와 59로 나누었을 때 error가 났다. 이때, 30과 60으로 나누는 대신, 기존값에 무조건 +1을 더해주는 식으로 고치면 해결되었다. 

<참고한 source code : https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x02/solutions/1267.cpp