https://www.acmicpc.net/problem/25206
> 풀이
C++에선 Split 관련한 함수를 제공하지 않기때문에 이를 구현해야 한다. 그래서 구성하기 쉬운 stringstream 클래스와 getline 함수를 이용하여 토큰값을 기준으로 나눠 스트링 벡터를 반환하는 함수를 구현 후 처리했다.
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cctype>
#include <vector>
#include <sstream>
using namespace std;
vector<string> split(string setence, char separator)
{
vector<string> answer;
stringstream ss(setence);
string temp;
while (getline(ss, temp, separator))
{
//cout << "t : " << temp << endl;
answer.push_back(temp);
}
return answer;
}
int main()
{
map<string, float> gradeMap = {
{"A+", 4.5},
{"A0", 4},
{"B+", 3.5},
{"B0", 3},
{"C+", 2.5},
{"C0", 2},
{"D+", 1.5},
{"D0", 1},
{"F", 0},
};
vector<string> list;
for (int i = 0; i < 20; i++)
{
string str;
getline(cin, str);
list.push_back(str);
}
float total = 0.0f;
float scoreTotal = 0.0f;
for (int i = 0; i < list.size(); i++)
{
vector<string> task = split(list[i], ' ');
float score = stof(task[1]);
if (gradeMap.find(task[2]) != gradeMap.end())
{
total += score * gradeMap[task[2]];
scoreTotal += score;
}
}
float avg = total / scoreTotal;
cout << avg;
return 0;
}