- 문제 링크 : boj.kr/1431
- 난이도 : S3
- 태그 : 정렬
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int sum(string &a) {
int ret = 0;
for(auto e : a) {
if(e <= '9') {
ret += e - '0';
}
}
return ret;
}
bool comp(string &a, string &b) {
if(a.length() != b.length()) {
return a.length() < b.length();
}
if(sum(a) != sum(b)) {
return sum(a) < sum(b);
}
return a < b;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<string> arr(n);
for(auto &e : arr) {
cin >> e;
}
sort(all(arr), comp);
for(auto e : arr) {
cout << e << "\n";
}
return 0;
}
풀이
주어진 조건을 반영하는 비교함수를 작성하여 정렬한다.
728x90
'PS' 카테고리의 다른 글
BOJ 2002 : 추월 (1) | 2024.12.09 |
---|---|
BOJ 32653 : 흑백 요리사 (0) | 2024.12.08 |
BOJ 5397 : 키로거 (0) | 2024.12.07 |
BOJ 32861 : 순열 복원 (1) | 2024.12.05 |
BOJ 16993 : 연속합과 쿼리 (0) | 2024.12.04 |