GitHubSeob
C++ / 프로그래머스 / 위클리 챌린지 6주차 / 복서 정렬하기 본문
문제
https://programmers.co.kr/learn/courses/30/lessons/85002
코딩테스트 연습 - 6주차
복서 선수들의 몸무게 weights와, 복서 선수들의 전적을 나타내는 head2head가 매개변수로 주어집니다. 복서 선수들의 번호를 다음과 같은 순서로 정렬한 후 return 하도록 solution 함수를 완성해주세요
programmers.co.kr
문제풀이
2차원 벡터를 선언하고 벡터[복서]에 4
첫 번째 정보는 이기면 Win의 개수를, 지면 lose개수를 늘리고 win/(win+lose)를 하여 복서의 정보를 저장한다.
두 번째 정보는 자신보다 무거운 복서를 이겼을 때의 개수를 저장한다.
세 번째 정보는 복서의 몸무게를 저장한다.
네 번째 정보는 복서의 번호를 저장한다.
첫 번째가 큰 순으로, 같으면 두 번째가 큰 순, 같으면 세 번째가 큰 순, 같으면 네 번째가 작은 순으로 정렬한다.
네 번째가 복서의 번호이므로 idx=0부터 vector [idx][3]를 answer에 push 한다.
코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool Compare(vector<double>a,vector<double>b){
if(a[0]==b[0]){
if(a[1]==b[1]){
if(a[2]==b[2]){
return a[3]<b[3];
}
else return a[2]>b[2];
}
else return a[1]>b[1];
}
else return a[0]>b[0];
}
vector<int> solution(vector<int> weights, vector<string> head2head) {
vector<vector<double>>records(weights.size(),vector<double>(0,0));
int idx=0;
int idx2=0;
for(idx=0;idx<head2head.size();++idx){
int win=0;
int lose=0;
int win_heavy=0;
for(idx2=0;idx2<head2head[idx].size();++idx2){
if(head2head[idx][idx2]=='W'){
win++;
if(weights[idx]<weights[idx2])
win_heavy++;
}
else if(head2head[idx][idx2]=='L')
lose++;
}
double rate=0;
if(win==0)rate=0;
else if(lose==0) rate=100;
else rate = (double)win / (lose + win);
records[idx].push_back(rate);
records[idx].push_back(win_heavy);
records[idx].push_back(weights[idx]);
records[idx].push_back(idx+1);
}
sort(records.begin(),records.end(),Compare);
vector<int> answer;
for(idx=0;idx<weights.size();++idx){
answer.push_back(records[idx][3]);
}
return answer;
}
'Programmers > Level 1' 카테고리의 다른 글
C++ / 프로그래머스 / 없는 숫자 더하기 (0) | 2021.09.13 |
---|---|
C++ / 프로그래머스 / 문자열을 정수로 바꾸기 (0) | 2021.09.13 |
C++ / 프로그래머스 / 수박수박수박수박수박수? (0) | 2021.09.06 |
C++ / 프로그래머스 / 소수 찾기 (0) | 2021.09.06 |
C++ / 프로그래머스 / 서울에서 김서방 찾기 (0) | 2021.09.06 |