728x90
반응형

심심하니까 푸는 프로그래머스 코테 문제

 

이번에 푼 문제는 '완주하지 못한 선수'

 

코딩테스트 연습 - 완주하지 못한 선수

수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수

programmers.co.kr

문제 캡쳐본

예전에 한 번 C++로 풀어봤다. 이런저런 방법으로 풀다가 결국 해설을 봤던 것 같다.

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

using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    string answer = "";
    sort(participant.begin(), participant.end());
    sort(completion.begin(), completion.end());
    
    for(int i = 0; i < participant.size(); i++){
        if(participant[i] != completion[i])
            return participant[i];
    }

    return participant[participant.size()];
}

 


 

이번에 고랭으로 풀어보려고 했는데 C++로 어떻게 풀었는지 이미 봐버려서 그대로 풀어버렸습니다 ㅠㅠ  근데 이 문제는 고랭은 지원하지 않아서 그냥 맞겠지 하고 풀었어요 ㅎㅎ

package main

import (
	"fmt"
	"sort"
)

func solution(participant []string, completion []string) string {
	sort.Strings(participant)
	sort.Strings(completion)
	
	for i:=0; i < len(completion); i++{
		if(participant[i] != completion[i]){
			return participant[i]
		}
	}
	return participant[len(participant)-1]
}

func main() {
	participant := []string{"leo", "kiki", "eden"}
	completion := []string{"eden", "kiki"}
	ret := solution(participant, completion)
	fmt.Println(ret)
}

sort 내장 모듈을 지원해주니 이 모듈을 사용했고 정확히 C++로 푼 방법과 똑같이 풀어버렸습니다 ㅋㅋㅋ 그래도 고랭 익숙해지는 거라고 생각하면 여전히 문제 풀어보는 건 좋지요!

728x90
반응형
728x90
반응형

programmers.co.kr/learn/courses/30/lessons/12939

 

코딩테스트 연습 - 최댓값과 최솟값

문자열 s에는 공백으로 구분된 숫자들이 저장되어 있습니다. str에 나타나는 숫자 중 최소값과 최대값을 찾아 이를 (최소값) (최대값)형태의 문자열을 반환하는 함수, solution을 완성하세요. 예를

programmers.co.kr

 

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

vector<string> split(string input, char delimiter) {
    vector<string> answer;
    stringstream ss(input);
    string temp;

    while (getline(ss, temp, delimiter)) {
        answer.push_back(temp);
    }
    return answer;
}

string solution(string s) {
    string answer = "";
    vector<string> nums = split(s, ' ');
    vector<int> num;
    int max = -9874;
    int min = 9874;

    for(int i= 0; i < nums.size(); i++){
        num.push_back(atoi(nums[i].c_str()));
        if(num[i] > max)
            max = num[i];
        if(num[i] < min)
            min = num[i];
    }
    answer = to_string(min) + " " + to_string(max);
    return answer;
}
728x90
반응형

+ Recent posts