๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Programmers)

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์ƒํ˜ธํ‰๊ฐ€( Level 1)

by ์•ˆ์ฃผํ˜• 2021. 10. 21.

์ฝ”๋“œ

#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

struct addScore{
    vector<int>score;   //ํ‰๊ท ์ ์ˆ˜ ๊ณ„์‚ฐํ•˜๋Š” ์ ์ˆ˜(์—ด)
};


char check(double avg){
    if(avg>=90) return 'A';
    else if(avg>=80&&avg<90) return 'B';
    else if(avg>=70&&avg<80) return 'C';
    else if(avg>=50&&avg<70) return 'D';
    else if(avg<50) return 'F';
    return 0;
}

string solution(vector<vector<int>> scores) {
    string answer = "";

    vector<addScore>addScore;
    map<int,int>m[500];

    for(int i=0; i<scores.size(); i++){
        vector<int>temp;
        for(int k=0; k<scores.size(); k++){
            temp.push_back(scores[k][i]);    //์„ธ๋กœ๋กœ ๋„ฃ์–ด์คŒ(์—ด).
            m[i][scores[k][i]]++;    //์ค‘๋ณต๋œ ์ ์ˆ˜ ์ฒดํฌ
        }
        addScore.push_back({temp});
    }


    for(int i=0; i<scores.size(); i++){

        double sum=0;
        int cnt=0;

        int max_value=*max_element(addScore[i].score.begin(), addScore[i].score.end());   //ํ•œ ํ•™์ƒ์ด ๋ฐ›์€ ์ตœ๊ณ ์ ์ˆ˜ ์ฐพ๊ธฐ
        int min_value=*min_element(addScore[i].score.begin(), addScore[i].score.end());   //ํ•œ ํ•™์ƒ์ด ๋ฐ›์€ ์ตœ์ €์ ์ˆ˜ ์ฐพ๊ธฐ

        for(int k=0; k<scores.size(); k++){
            if(i==k){
                if(scores[k][i]==max_value){  //๋ณธ์ธ์—๊ฒŒ ์ตœ๊ณ ์ ์ˆ˜๋ฅผ ๋งค๊ฒจ์คฌ์„๋•Œ
                    if(m[i][scores[k][i]]!=1){
                        sum+=scores[k][i];  //์ค‘๋ณต๋œ ์ ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ํ‰๊ท ๋‚ด์คŒ
                        cnt++;
                    }
                }
                else if(scores[k][i]==min_value){ //๋ณธ์ธ์—๊ฒŒ ์ตœ์ €์ ์ˆ˜๋ฅผ ๋งค๊ฒจ์คฌ์„๋•Œ
                    if(m[i][scores[k][i]]!=1){
                        sum+=scores[k][i];  //์ค‘๋ณต๋œ ์ ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ํ‰๊ท ๋‚ด์คŒ
                        cnt++;
                    }
                }
                else{
                    sum+=scores[k][i];
                    cnt++;
                }
            }
            else{
                sum+=scores[k][i];
                cnt++;
            }
        }

        sum/=cnt;
        answer+=check(sum);

    }
    return answer;
}

๋Œ“๊ธ€