๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 1431๋ฒˆ - ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ

by dkswnkk 2021. 11. 7.
 

14391๋ฒˆ: ์ข…์ด ์กฐ๊ฐ

์˜์„ ์ด๋Š” ์ˆซ์ž๊ฐ€ ์“ฐ์—ฌ ์žˆ๋Š” ์ง์‚ฌ๊ฐํ˜• ์ข…์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ข…์ด๋Š” 1×1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๊ณ , ์ˆซ์ž๋Š” ๊ฐ ์นธ์— ํ•˜๋‚˜์”ฉ ์“ฐ์—ฌ ์žˆ๋‹ค. ํ–‰์€ ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๊ณ ,

www.acmicpc.net

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool cmp(string a, string b) {
    if (a.length() != b.length()) return a.length() < b.length();    //๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ๊ฒฝ์šฐ ์งง์€ ๊ฒƒ ๋ถ€ํ„ฐ ๋‚˜์—ด

    else  { //๊ธธ์ด๊ฐ€ ๊ฐ™์€๊ฒฝ์šฐ 
        int sumA=0,sumB= 0;
        for (int i = 0; i <a.length(); i++) {
            if (a[i] - '0' >= 0 && a[i] - '0' <= 9) sumA += a[i] - '0';
            if (b[i] - '0' >= 0 && b[i] - '0' <= 9) sumB += b[i] - '0';
        }
        if (sumA != sumB) return  sumA < sumB;    // ํ•ฉ์ด ์ž‘์€๊ฒŒ ์•ž์œผ๋กœ
        else return a < b;    //ํ•ฉ์ด ๊ฐ™์„๊ฒฝ์šฐ ์‚ฌ์ „์ˆœ์œผ๋กœ
    }

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int N; cin >> N;
    vector<string>v;
    while (N--) {
        string s; cin >> s;
        v.push_back(s);
    }
    sort(v.begin(), v.end(), cmp);

    for (string s : v) {
        cout << s << "\n";
    }
}

๋Œ“๊ธ€