※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 27106번 문제인 Making Change이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/27106
27106번: Making Change
Given the amount of a purchase (1 ≤ P ≤ 99) in cents, determine the way to make "change for a dollar" for that purchase. Use four standard US coin denominations: 1, 5, 10, and 25. The way to make change uses the least number coins.
www.acmicpc.net
내야 하는 비용 P센트가 주어질 때, 1달러를 지불했을 때 받을 거스름돈을 가장 적은 동전의 개수로 나타내는 문제이다.
거스름돈은 1달러, 즉 100센트에서 P를 빼는 것으로 계산할 수 있다.
가장 큰 단위의 동전부터, 즉 25 - 10 - 5 - 1센트 동전 순으로 각각 최대한 많이 사용해 거스름돈 액수를 최대한 채워나가는 전략으로 문제를 해결하자.
아래는 제출한 소스코드이다.
#include <iostream>
using namespace std;
int P;
int a, b, c, d;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> P;
P = 100 - P;
while (P > 24) a++, P -= 25;
while (P > 9) b++, P -= 10;
while (P > 4) c++, P -= 5;
while (P) d++, P--;
cout << a << ' ' << b << ' ' << c << ' ' << d;
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 27101 // C++] Metric Matrices (0) | 2023.01.04 |
---|---|
[BOJ 5938 // C++] Daisy Chains in the Field (0) | 2023.01.04 |
[BOJ 17141 // C++] 연구소 2 (0) | 2023.01.03 |
[BOJ 24198 // C++] Muffinspelet (0) | 2023.01.02 |
[BOJ 17142 // C++] 연구소 3 (0) | 2023.01.02 |