※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 5342번 문제인 Billing이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/5342
5342번: Billing
The accounting office is having troubling with billing each department for the supplies purchased last semester. Below is a chart of all the supplies and how much they cost. You must write a program that reads a list of the supplies and computes the total
www.acmicpc.net
물품별 가격을 대응시켜두고 물품을 입력할 때마다 답에 해당 물품의 가격을 더하는 식으로 구현해 문제를 해결하자.
map을 이용하면 물품과 가격을 간편하게 대응시켜둘 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef long double ld;
map<string, ld> mp;
ld ans;
string s;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
mp.insert({ "Paper",57.99 });
mp.insert({ "Printer", 120.50 });
mp.insert({ "Planners",31.25 });
mp.insert({ "Binders",22.50 });
mp.insert({ "Calendar",10.95 });
mp.insert({ "Notebooks",11.20 });
mp.insert({ "Ink",66.95 });
cin >> s;
while (s != "EOI") {
ans += mp[s];
cin >> s;
}
cout << fixed;
cout.precision(2);
cout << '$' << ans;
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 26587 // C++] Reverse (0) | 2022.12.22 |
---|---|
[BOJ 5340 // C++] Secret Location (0) | 2022.12.22 |
[BOJ 5292 // C++] Counting Swann's Coins (0) | 2022.12.22 |
[BOJ 15179 // C++] Golf Croquet (0) | 2022.12.22 |
[BOJ 5360 // C++] Next Permutation (0) | 2022.12.22 |