문제 링크 : https://mungto.tistory.com/22

 

가장 큰 수 C++ (정렬)[프로그래머스]

※ 저의 풀이가 무조건적인 정답은 아닙니다. 다른 코드가 좀더 효율적이고 좋을 수 있습니다. 다른사람들의 풀이는 언제나 참고만 하시기 바랍니다. 문제 주소입니다. https://programmers.co.kr/learn/courses/3..

mungto.tistory.com

 

 문제 설명 

 

0또는 양의 정수로 이루어진 벡터가 주어지는데 이벡터의 숫자들로 만들수 있는 최대값을 return.

 

 알고리즘 

 

1. 정렬을 할때 서로 붙였을 때 값이 큰순 서로 정렬을 한다 ( 커스텀 함수 )

  -ex ) 6,10 -> 610 > 106

2. 정렬을하고 앞에서부터 문자열을 붙여서 출력 하면된다.

 

커스텀함수 제작에서 깊은감명을 받은 블로그 : https://mungto.tistory.com/22

 

 코드 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <vector>
#include<iostream>
#include<algorithm>
using namespace std;
 
 
 
 
 
bool func(string a, string b) {
    return a + b > b + a;
}
 
string solution(vector<int> numbers) {
    int Size = numbers.size();
    vector <string> str;
    string answer;
    for (int i = 0; i < Size; i++)
        str.push_back(to_string(numbers[i]));
    
    sort(str.begin(), str.end(),func);
    if (str[0== "0"return "0";
        for (int i = 0; i < Size; i++) {
            answer += str[i];
    }
    return answer;
}
 
cs
 

 

'c++ > 프로그래머스' 카테고리의 다른 글

프로그래머스 : 카펫  (0) 2020.04.25
프로그래머스 : 소수 찾기  (0) 2020.04.24
프로그래머스 : H-Index  (0) 2020.04.24
프로그래머스: 종이접기  (0) 2020.04.09
프로그래머스 : 서울에서 경산까지  (0) 2020.04.07
ariz1623