baekjoon

16496번 큰 수 만들기

gokimkh 2022. 4. 15. 21:36

https://www.acmicpc.net/problem/16496

 

16496번: 큰 수 만들기

첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 리스트에 포함된 수가 주어진다. 수는 공백으로 구분되어져 있고, 1,000,000,000보다 작거나 같은 음이 아닌 정수 이다. 0을 제외한 나

www.acmicpc.net

그리디, 정렬 문제이다. 약간의 꼼수를 써서 string a, string b가 있다고 할때 a와 b를 붙인게 b와 a를 붙인 거보다 크면 true 

아니면 false 반환했다. 예를 들어 a = "100"이고 b = "32"일 때 a + b = "10032"이고, b + a = "32100"이므로 이 값들을 비교하였다.

 

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define INF 2e9
#define F first
#define S second
using namespace std;

typedef long long l;
typedef pair<int,int> p;
typedef tuple<int,int,int> tu;
typedef vector<string> vc;

int main() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    vc v;

    int n;
    string s;

    cin >> n;

    for(int i=0;i<n;i++){
        cin >> s;
        v.push_back(s);
    }

    sort(all(v),[](string a,string b){
        return a + b > b + a;
    });

    if(v[0][0] == '0') cout << "0";
    else{
        for(auto &i : v)
            cout << i;
    }
}