Skip to content

Commit

Permalink
Merge pull request #66 from SSAFY-10th/algo/chjcode/boj1431
Browse files Browse the repository at this point in the history
[python]boj1431
  • Loading branch information
chjcode authored Sep 19, 2023
2 parents e5a6558 + 709fe9a commit 6c58810
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
61 changes: 61 additions & 0 deletions seoul_x_hwijoonchoi/BOJ/boj1431/README.md
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)

```

## 결과

결과 : [맞았습니다]
41 changes: 41 additions & 0 deletions seoul_x_hwijoonchoi/BOJ/boj1431/boj1431.py
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)

0 comments on commit 6c58810

Please sign in to comment.