#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <numeric>
#include <math.h>
#include <algorithm>
#include <functional>
using namespace std;
bool compare(const string &a, const string &b) {
if(a.length() != b.length()) return a.length() < b.length();
else {
int sum_A = 0;
int sum_B = 0;
for(int i = 0; i < a.length(); i++) {
if(isdigit(a[i])) sum_A += (a[i] - '0');
if(isdigit(b[i])) sum_B += (b[i] - '0');
}
return sum_A == sum_B ? a < b : sum_A < sum_B;
}
}
int main() {
int N;
cin >> N;
vector<string> arr(N);
string temp = "";
for(int i = 0; i < N; i++) {
cin >> temp;
arr[i] = temp;
}
sort(arr.begin(), arr.end(), compare);
for(string it : arr)
cout << it << "\n";
}
솔팅 할 때 compare 함수를 살짝 손봤습니다.
길이가 같지 않다면 길이비교를, 아니면 숫자만 골라서 숫자 합의 결과를 그것도 같다면 그냥 사전 순으로 비교하였습니다.
문제: https://www.acmicpc.net/problem/1431
1431번: 시리얼 번호
첫째 줄에 기타의 개수 N이 주어진다. N은 50보다 작거나 같다. 둘째 줄부터 N개의 줄에 시리얼 번호가 하나씩 주어진다. 시리얼 번호의 길이는 최대 50이고, 알파벳 대문자 또는 숫자로만 이루어
www.acmicpc.net
'Problem Solving > 백준' 카테고리의 다른 글
[백준/C++] 2941번 크로아티아 알파벳 (0) | 2022.02.10 |
---|---|
[백준/C++] 1755번 숫자놀이 (0) | 2022.02.10 |
[백준/C++] 11656번 접미사 배열 (0) | 2022.02.10 |
[백준/C++] 10867번 중복 빼고 정렬하기 (0) | 2022.02.10 |
[백준/C++] 10814번 나이순 정렬 (0) | 2022.02.10 |