16496 큰 수 만들기

2019. 10. 21. 22:31알고리즘/백준

정렬 문제로, 두 수를 정렬할 때 34, 3이라고 하면 343 > 334이므로 34, 3으로 정렬해야만 큰 수를 뽑아낼 수 있다

 

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

깃허브주소: https://github.com/surinoel/boj/blob/master/16496.cpp

 

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int arr[1000];
bool cmp(const int &u, const int &v) {
string a = to_string(u) + to_string(v);
string b = to_string(v) + to_string(u);
return a > b;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n, cmp);
string ans = "";
for (int i = 0; i < n; i++) {
ans += to_string(arr[i]);
}
if (ans.length() > 1 && ans[0] == '0') {
ans = "0";
}
cout << ans << '\n';
return 0;
}
view raw 16496.cpp hosted with ❤ by GitHub

 

 

'알고리즘 > 백준' 카테고리의 다른 글

16933 벽 부수고 이동하기 3  (0) 2019.10.22
1652 누울 자리를 찾아라  (0) 2019.10.22
2458 키 순서  (0) 2019.10.19
2010 플러그  (0) 2019.10.18
1918 후위 표기식  (0) 2019.10.17