Study hard

프로그래머스 코딩테스트 연습 - 2주차_상호평가 본문

프로그래머스/위클리 챌린지

프로그래머스 코딩테스트 연습 - 2주차_상호평가

Nimgnoej 2021. 10. 8. 15:24

https://programmers.co.kr/learn/courses/30/lessons/83201#

 

코딩테스트 연습 - 2주차_상호평가

[[100,90,98,88,65],[50,45,99,85,77],[47,88,95,80,67],[61,57,100,80,65],[24,90,94,75,65]] "FBABD" [[70,49,90],[68,50,38],[73,31,100]] "CFD"

programmers.co.kr

[풀이]

i번 학생이 자신에게 준 점수가 자신이 받은 점수 중 유일한 최저점이거나 유일한 최고점이면 평균을 구하는 식에서 제외한다.

 

#include <string>
#include <vector>
#include <algorithm>//sort

using namespace std;

string solution(vector<vector<int>> scores) {
    string answer = "";
    int N=scores[0].size();
    for(int i=0;i<N;i++){
        //i번 학생이 자신에게 준 점수
        int mine=scores[i][i];
        vector<int>tmp;
        double score=0;
        int cnt=0;
        for(int j=0;j<N;j++){
            tmp.push_back(scores[j][i]);
            score+=scores[j][i];
            cnt++;
        }
        sort(tmp.begin(),tmp.end());
        //유일한 최소인지
        if(tmp.front()==mine&&mine!=tmp[1]){
            score-=mine;
            cnt--;
        }
        //유일한 최대인지
        else if(tmp.back()==mine&&mine!=tmp[N-2]){
            score-=mine;
            cnt--;
        }
        score/=cnt;
        if(score>=90)
            answer+='A';
        else if(score>=80&&score<90)
            answer+='B';
        else if(score>=70&&score<80)
            answer+='C';
        else if(score>=50&&score<70)
            answer+='D';
        else
            answer+='F';
    }
    return answer;
}