https://www.acmicpc.net/problem/1431
정렬 기준 만들어주기
<숫자인 것만 더하는 조건>
if(s[i] - '0' >= 0 && s[i] - '0' <= 9) { sum += s[i] - '0'; }
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
vector<string> v;
int getSum(string s) {
int sum=0;
for(int i=0; i<s.length(); i++) {
if(s[i] - '0' >= 0 && s[i] - '0' <= 9) {
sum += s[i] - '0';
}
}
return sum;
}
bool compare(string a, string b) {
if(a.length() != b.length())
return a.length() < b.length();
else {
int aSum = getSum(a);
int bSum = getSum(b);
if(aSum != bSum)
return aSum < bSum;
else
return a<b;
}
}
int main(void) {
int number;
string input;
scanf("%d", &number);
for(int i=0; i<number; i++) {
cin >> input;
v.push_back(input);
}
sort(v.begin(), v.end(), compare);
for(int i=0; i<number; i++)
cout << v[i] << endl;
return 0;
}