ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/1431
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var serials = make([]string, n)
for i := 0; i < n; i++ {
fmt.Fscanln(reader, &serials[i])
}
sort.Slice(serials, func(i, j int) bool {
if len(serials[i]) < len(serials[j]) {
return true
} else if len(serials[i]) == len(serials[j]) {
var sumI, sumJ int
for k := 0; k < len(serials[i]); k++ {
if val, err := strconv.Atoi(string(serials[i][k])); err == nil {
sumI += val
}
if val, err := strconv.Atoi(string(serials[j][k])); err == nil {
sumJ += val
}
}
if sumI < sumJ {
return true
} else if sumI == sumJ {
return serials[i] < serials[j]
}
return false
}
return false
})
for i := 0; i < n; i++ {
fmt.Fprintln(writer, serials[i])
}
}
๐จ Python3
# https://www.acmicpc.net/problem/1431
import sys
if __name__ == "__main__":
n = int(sys.stdin.readline())
serials = []
for i in range(n):
serials.append(sys.stdin.readline().rstrip())
serials = sorted(serials, key=lambda s: (len(s), sum([int(i) for i in s if type(i)== int or i.isdigit()]), s))
for i in serials:
print(i)
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 11725๋ฒ / ํธ๋ฆฌ์ ๋ถ๋ชจ ์ฐพ๊ธฐ [Go][Python3] (0) | 2021.04.02 |
---|---|
BOJ / 2343๋ฒ / ๊ธฐํ ๋ ์จ [Go][Python3] (0) | 2021.04.01 |
BOJ / 11055๋ฒ / ๊ฐ์ฅ ํฐ ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด [Go][Python3] (0) | 2021.03.31 |
BOJ / 11722๋ฒ / ๊ฐ์ฅ ๊ธด ๊ฐ์ํ๋ ๋ถ๋ถ ์์ด [Go][Python3] (0) | 2021.03.30 |
BOJ / 10974๋ฒ / ๋ชจ๋ ์์ด [Go][Python3] (0) | 2021.03.29 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ๋ฐ๋
- MongoDB
- ํ๋ก์ด๋์์ฌ
- ์๋ฐ
- ์คํ
- ํด์๋งต
- ํ
- BOJ
- ์ด๋ถํ์
- ์๊ณ ๋ฆฌ์ฆ
- python3
- ์๊ฐ๊ต์ฒด
- Macbook pro 2012 mid 13
- ๋ถํ ์ ๋ณต
- Golang
- ๋งฅ๋ถ
- AWS
- Algorithm
- dp
- java
- BFS
- ๋ธ๋ฃจํธํฌ์ค
- go
- dfs
- ๋ฐฑ์ค
- baekjoon
- ๋ชฝ๊ณ ๋๋น
- ๋งฅ๋ถํ๋ก
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ballet
- Total
- Today
- Yesterday