백준 14916 c++

magicdrill·2024년 2월 28일

백준 문제풀이

목록 보기
51/675

백준 14916 c++

최소 개수를 구하려면 5원을 최대한 많이 거슬러주어야 한다.
5의 처음 개수를 구하고, 2원으로 거스름돈을 맞출 수 없을 경우 5원을 다시 추가하고 반복한다.
5원 개수가 -1이 될 때까지 결과를 만들지 못하면 2원 개수가 0이 되기에 -1을 출력한다.

#include <iostream>

using namespace std;

void find_answer(int n)
{
	int five_cnt = 0, two_cnt = 0;
	int num = n;

	five_cnt = num / 5;
	num = num % 5;
	while (five_cnt >= 0)
	{
		if (num % 2 == 0)
		{
			two_cnt = num / 2;
			break;
		}
		else
		{
			five_cnt--;
			num = num + 5;
		}
	}
	//cout << five_cnt << " " << two_cnt << "\n";
	cout << five_cnt + two_cnt << "\n";

	return;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int n;

	cin >> n;
	find_answer(n);

	return 0;
}

0개의 댓글