반응형
https://programmers.co.kr/learn/courses/30/lessons/42746
코딩테스트 연습 - 가장 큰 수
0 또는 양의 정수가 주어졌을 때, 정수를 이어 붙여 만들 수 있는 가장 큰 수를 알아내 주세요. 예를 들어, 주어진 정수가 [6, 10, 2]라면 [6102, 6210, 1062, 1026, 2610, 2106]를 만들 수 있고, 이중 가장 큰
programmers.co.kr
글쓴이의 답
개인적인 풀이 임으로
이것보다 더 좋은 알고리즘은 많음...
이렇게도 풀이하는구나.. 공유하기 위해 올림...
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
string solution(vector<int> numbers) {
string answer = "";
multimap<string, string, greater<string>> save;
for(int temp : numbers){
string tempS = to_string(temp);
size_t sizeS = tempS.size();
int j=0;
for(size_t i=tempS.size(); i < 6; i++){
tempS += tempS[j];
j++;
if(j == sizeS){
j=0;
}
}
save.insert(pair<string, string>(tempS, to_string(temp)));
}
for(const auto &temp : save){
if((answer.size() == 0) && (stoi(temp.second) ==0)){
answer ="0";
break;
}
answer += temp.second;
}
return answer;
}
꾸준히 하다보면 실력이 늘겠지..
반응형
'코딩테스트' 카테고리의 다른 글
[프로그래머스][C++] 조이스틱 (0) | 2022.03.22 |
---|---|
[프로그래머스][C++] 소수 찾기 (0) | 2022.03.22 |
[프로그래머스][C++] 프린터 (0) | 2022.03.21 |
[프로그래머스][C++] 전화번호 목록 (0) | 2022.03.21 |
[프로그래머스][C++] 빛의 경로 사이클 (0) | 2022.03.21 |