본문 바로가기
알고리즘 풀이

너의 평점은

by 남생이야 2024. 7. 30.

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; 
}

'알고리즘 풀이' 카테고리의 다른 글

N과 M (1)  (0) 2024.08.07
N과 M (3)  (0) 2024.08.07
그룹 단어 체커  (0) 2024.07.29
크로아티아 알파벳  (0) 2024.07.29
1157 단어 공부  (0) 2024.07.29