문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/85002
코딩테스트 연습 - 6주차
복서 선수들의 몸무게 weights와, 복서 선수들의 전적을 나타내는 head2head가 매개변수로 주어집니다. 복서 선수들의 번호를 다음과 같은 순서로 정렬한 후 return 하도록 solution 함수를 완성해주세요
programmers.co.kr
상당히 쉬운 문제였습니다.
문제에서 주어진 대로 정렬을 진행하면 되는 문제였으며 코드를 보시면 쉽게 이해가 될 것으로 판단됩니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
typedef struct | |
{ | |
int idx; | |
int weight; | |
double winRatio; | |
int winAgainstHeavier; | |
} Boxer; | |
bool cmp(Boxer a, Boxer b) | |
{ | |
if (a.winRatio > b.winRatio) | |
{ | |
return true; | |
} | |
if (a.winRatio == b.winRatio) | |
{ | |
if (a.winAgainstHeavier > b.winAgainstHeavier) | |
{ | |
return true; | |
} | |
if (a.winAgainstHeavier == b.winAgainstHeavier) | |
{ | |
if (a.weight > b.weight) | |
{ | |
return true; | |
} | |
if (a.weight == b.weight && a.idx < b.idx) | |
{ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
vector<int> solution(vector<int> weights, vector<string> head2head) { | |
vector<Boxer> boxers; | |
for (int i = 0; i < head2head.size(); i++) | |
{ | |
double wins = 0; | |
double total = 0; | |
int winAgainstHeavier = 0; | |
for (int j = 0; j < head2head[i].length(); j++) | |
{ | |
if (head2head[i][j] == 'N') | |
{ | |
continue; | |
} | |
total++; | |
if (head2head[i][j] == 'W') | |
{ | |
wins++; | |
if (weights[i] < weights[j]) | |
{ | |
winAgainstHeavier++; | |
} | |
} | |
} | |
boxers.push_back({ i + 1, weights[i], total ? wins / total : 0, winAgainstHeavier }); | |
} | |
sort(boxers.begin(), boxers.end(), cmp); | |
vector<int> answers; | |
for (Boxer boxer : boxers) | |
{ | |
answers.push_back(boxer.idx); | |
} | |
return answers; | |
} | |
int main(void) | |
{ | |
vector<int> weights = { 60, 70, 60 }; | |
vector<string> head2head = { "NNN", "NNN", "NNN" }; | |
vector<int> answers = solution(weights, head2head); | |
for (int answer : answers) | |
{ | |
cout << answer << " "; | |
} | |
cout << "\n"; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers 코딩테스트 고득점 Kit] 네트워크 (0) | 2021.09.08 |
---|---|
[Programmers 코딩테스트 고득점 Kit] 타겟 넘버 (0) | 2021.09.07 |
[Programmers 위클리 챌린지 5주차] 모음 사전 (0) | 2021.09.04 |
[Programmers 위클리 챌린지 4주차] 직업군 추천하기 (0) | 2021.09.04 |
[Programmers 위클리 챌린지 3주차] 퍼즐 조각 채우기 (0) | 2021.09.04 |