-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from SSAFY-10th/algo/chjcode/boj1431
[python]boj1431
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# [BoJ 2194 bfs](https://www.acmicpc.net/problem/2194) | ||
|
||
solved.ac silver 3 | ||
|
||
## 카테고리 | ||
|
||
정렬 | ||
|
||
## 시간복잡도 | ||
|
||
O(logn) | ||
|
||
## 풀이 | ||
|
||
```python | ||
# 1431 시리얼 번호 | ||
|
||
n = int(input()) | ||
serial = [input() for _ in range(n)] | ||
|
||
from functools import cmp_to_key | ||
|
||
def compare(a,b): | ||
len_a = len(a) | ||
len_b = len(b) | ||
num = ['0','1','2','3','4','5','6','7','8','9'] | ||
# 첫번째 조건 | ||
if len_a > len_b: # a가 큰데 자리를 바꾸면 큰 값이 뒤로 오게 정렬됨 | ||
return 1 | ||
elif len_a < len_b: | ||
return -1 | ||
|
||
# 두번째 조건 | ||
cnt_a = 0 | ||
cnt_b = 0 | ||
for i in range(len_a): | ||
if a[i] in num: | ||
cnt_a += int(a[i]) | ||
if b[i] in num: | ||
cnt_b += int(b[i]) | ||
if cnt_a > cnt_b: | ||
return 1 | ||
elif cnt_a < cnt_b: | ||
return -1 | ||
# 세번째 조건 | ||
if a > b: | ||
return 1 | ||
else: | ||
return -1 | ||
|
||
|
||
serial = sorted(serial, key = cmp_to_key(compare)) | ||
|
||
for temp in serial: | ||
print(temp) | ||
|
||
``` | ||
|
||
## 결과 | ||
|
||
결과 : [맞았습니다] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 1431 시리얼 번호 | ||
|
||
|
||
n = int(input()) | ||
serial = [input() for _ in range(n)] | ||
|
||
from functools import cmp_to_key | ||
|
||
def compare(a,b): | ||
len_a = len(a) | ||
len_b = len(b) | ||
num = ['0','1','2','3','4','5','6','7','8','9'] | ||
# 첫번째 조건 | ||
if len_a > len_b: # a가 큰데 자리를 바꾸면 큰 값이 뒤로 오게 정렬됨 | ||
return 1 | ||
elif len_a < len_b: | ||
return -1 | ||
|
||
# 두번째 조건 | ||
cnt_a = 0 | ||
cnt_b = 0 | ||
for i in range(len_a): | ||
if a[i] in num: | ||
cnt_a += int(a[i]) | ||
if b[i] in num: | ||
cnt_b += int(b[i]) | ||
if cnt_a > cnt_b: | ||
return 1 | ||
elif cnt_a < cnt_b: | ||
return -1 | ||
# 세번째 조건 | ||
if a > b: | ||
return 1 | ||
else: | ||
return -1 | ||
|
||
|
||
serial = sorted(serial, key = cmp_to_key(compare)) | ||
|
||
for temp in serial: | ||
print(temp) |