본문 바로가기

학부 수업 정리/알고리즘연습 (22-1)

[알고연] (1주차-1) 4A. Watermelon

4A. Watermelon

One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.

Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.

Codeforces의 기초 문제로 과거에 처음 가입했을 때 한번 풀어봤던 문제다. 성급하게 홀 짝만 판단하면 되겠지 생각하고 코드를 짜면 Wrong answer on test 5 가 뜨게 된다. 수박이 2개인 경우, 1개 1개로 분할되기 때문이다. 이 예외만 잘 고려해서 처리해주면 통과할 수 있다.

#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
	cin.tie(NULL);	cout.tie(NULL);	std::ios::sync_with_stdio(false);
 
	int w;
	cin >> w;
	if (w < 4) cout << "NO\n";
	else if (w % 2 == 0) cout << "YES\n";
	else cout << "NO\n";
	return 0;
}