Skip to content

Commit 15e9b1a

Browse files
committed
큰 수로 만들기
1 parent 8f22e47 commit 15e9b1a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

challenges/BOJ16496.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 문제
2+
큰 수 만들기
3+
## 문제 원본
4+
문제의 원본은 [여기서](https://www.acmicpc.net/problem/16496) 확인하세요.
5+
6+
## 분류
7+
* 정렬
8+
9+
# 풀이
10+
11+
단순히 크기로 비교를 하면 문제가 발생한다. 예를들어 3, 30이 있다. 303보다 330 이 큼으로 단순 크기비교는 불가능하다. 따라서 "3" + "30" 과 "30" + "3" 처럼 문자열로 바꿔 더한 다음 비교한다. 즉 330 과 303을 비교했을때 330이 더 큰 수 이므로 3이 앞으로 간다. 이러한 비교방식으로 내림차순 정렬을 한후 출력한다.
12+
13+
``` c++
14+
#include <iostream>
15+
#include <algorithm>
16+
#include <vector>
17+
#include <string>
18+
19+
using namespace std;
20+
21+
bool compare(int a, int b) {
22+
string _a = to_string(a) + to_string(b);
23+
string _b = to_string(b) + to_string(a);
24+
25+
return _a > _b;
26+
}
27+
28+
int main(void) {
29+
int n;
30+
cin >> n;
31+
32+
vector<int> nums(n);
33+
34+
for (int i = 0; i < n; i++) {
35+
cin >> nums[i];
36+
}
37+
38+
sort(nums.begin(), nums.end(), compare);
39+
40+
if (nums[0] == 0) {
41+
cout << 0;
42+
return 0;
43+
}
44+
45+
for (int i = 0; i < n; i++) {
46+
cout << nums[i];
47+
}
48+
49+
return 0;
50+
}
51+
```

0 commit comments

Comments
 (0)