ALGORITHM

[1431] 시리얼번호

밍디 ٩(ˊ ᗜˋ*)و 2020. 2. 19. 22:20

비교함수 만들기 힘들다

 

이리보니까 이래 간단한데...

#include <iostream>
#include <deque>
#include <string>
#include <algorithm>
using namespace std;
 
struct cmp {
    bool operator()(string first, string second) {
        // 길이 작은순
        int n = 0, m = 0;
 
        if (first.length() != second.length()) {
            return first.length() < second.length();
        }
 
        for (string::size_type i = 0; i < first.length(); i++) {
            char c = first[i];
            if (c - '0' < 10) {
                n += c - '0';
            }
            c = second[i];
            if (c - '0' < 10) {
                m += c - '0';
            }
        }
 
        if (n != m) {
            return n < m;
        }
        else {
            return first < second;
        }
 
        return false;
 
    }
};
int main() {
 
    deque<string> dq;
    int num;
    scanf("%d", &num);
    for (int i = 0; i < num; i++) {
        string str;
        cin >> str;
        dq.push_back(str);
    }
    sort(dq.begin(), dq.end(), cmp());
    for (int i = 0; i < num; i++) {
        cout << dq[i] << endl;
    }
    
}