[프로그래머스 / C++] 실패율

Taegang Yun·2023년 9월 1일
1

2019 KAKAO BLIND RECRUITMENT
https://school.programmers.co.kr/learn/courses/30/lessons/42889#

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

using namespace std;

bool cmp(pair<int, double> a, pair<int, double> b)
{
    if(a.second == b.second){
        return a.first < b.first;
    }
    else return a.second > b.second;
}


vector<int> solution(int N, vector<int> stages) {
    vector<pair<int, double>> tmp;
    
    vector<int> answer;
    int cnt;
    double ErrorValue;
    int NumberOfPlayers = stages.size();
    
    int CurrentPlayers = NumberOfPlayers;
    for(int i = 1 ; i <= N; i++)
    {
        cnt = 0;
        for(int j = 0 ; j < NumberOfPlayers; j++)
        {
            if (stages[j] == i) cnt++;
        }

        ErrorValue = (double)cnt / (double)CurrentPlayers;
        CurrentPlayers -= cnt;
        tmp.push_back({i, ErrorValue});
    }
    

    
    sort(tmp.begin(), tmp.end(), cmp);
    for(int i = 0 ; i < tmp.size(); i++){
        answer.push_back(tmp[i].first);
    }
    
    return answer;
}

처음엔 이렇게 풀었는데, 81점이 나왔다. 잉? 왜 틀렸을까..

그래서 사람들 질문을 좀 봤는데 cnt = 0일 때를 잘 처리해주어야 한다더라.

그래서

        if(cnt == 0)
        {
            ErrorValue = 0;
            tmp.push_back({i, 0});
            continue;
        }

이 조건 하나를 붙여줬더니 100점이 나왔다. 근데 의문이다.
원래는 왜 안됬던 거지?
똑같이 ErrorValue = 0일거고, (cnt == 0 이니까)
CurrentPlayers 에서 안 빠질거고.. 음... 의문인데 진짜?

그리고 몰랐던 게 하나 있었는데

int 형 끼리는 아무리 / 을 해도 무조건 int형만들 반환한다. 음 몰랐네
나는 double ErrorValue = 3 / 7 이런식으로 하면 당연히 3/7 이 들어갈 줄 알았는데 디버그 해보면 0 이 들어가더라.
그래서 (double) 이런 식으로 캐스팅 해주고 해줘야 내가 원하는 대로 들어간다.

어쨌든 해결!

profile
언젠간 전문가가 되겠지

0개의 댓글

관련 채용 정보