Outdated/Algorithm Solution

[BOJ] 1431번 시리얼 번호

해달 2020. 4. 20. 08:00

복기

복잡한 정렬에서는 나중 조건부터 차례대로 정렬해나가면 깔끔한 코드를 작성할 수 있다. 그리고 항상 모듈을 올바르게 구현했는지 확인하자.


코드

  • C++ 17


#include <iostream>

#include <string>

#include <algorithm>

 

using namespace std;

 

int N;

string Serials[1000];

 

int Cal(const string& str)

{

    int result = 0;

    for (char ch : str)

    {

        if (isdigit(ch))

            result += ch - '0';

    }

 

    return result;

}

 

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

 

    cin >> N;

    for (size_t i = 0; i < N; i++)

        cin >> Serials[i];

 

    sort(Serials, Serials + N, [](const auto& lhs, const auto& rhs)

    {

        if (lhs.size() < rhs.size())

            return true;

 

        if (lhs.size() == rhs.size())

        {

            int l = Cal(lhs);

            int r = Cal(rhs);

            if (l < r)

                return true;

 

            if (l == r)

                return lhs < rhs;

        }

        

        return false;

    });

 

    for (size_t i = 0; i < N; i++)

        cout << Serials[i] << '\n';

}


  • C# 6.0


using System;

 

namespace Csharp

{

    class Program

    {

        static void Main(string[] args)

        {

            int N = Convert.ToInt32(Console.ReadLine());

            string[] Serials = new string[N];

            for (int i = 0; i < N; i++)

            {

                Serials[i] = Console.ReadLine();

            }

 

            Array.Sort(Serials, (lhs, rhs) =>

            {

                int lsum = Cal(lhs), rsum = Cal(rhs);

                int leftLen = lhs.Length, rightLen = rhs.Length;

 

                if (leftLen == rightLen && lsum == rsum)

                    return lhs.CompareTo(rhs);

 

                if (leftLen == rightLen)

                    return lsum.CompareTo(rsum);

 

                return leftLen.CompareTo(rightLen);

            });

 

            for (int i = 0; i < N; i++)

                Console.WriteLine(Serials[i]);

        }

        

        static int Cal(string str)

        {

            int result = 0;

            for (int i = 0; i < str.Length; i++)

            {

                if (Char.IsDigit(str[i]))

                    result += str[i] - '0';

            }

 

            return result;

        }

    }

}


  • Python 3


from sys import stdin

input = stdin.readline

 

def cal(s):

    result = 0

    for ch in s:

        if ch.isdigit():

            result += int(ch)

    

    return result

 

N = int(input())

serials = [''] * N

 

for i in range(N):

    serials[i] = input().rstrip()

 

serials.sort()

serials.sort(key = lambda item: cal(item))

serials.sort(key = lambda item: len(item))

 

output = '\n'.join(serials)

print(output)


'Outdated > Algorithm Solution' 카테고리의 다른 글

[BOJ] 10174번 팰린드롬  (0) 2020.04.27
[BOJ] 1083번 소트  (0) 2020.04.20
[BOJ] 3101번 토끼의 이동  (0) 2020.04.13
[BOJ] 4948번 베르트랑 공준  (0) 2020.04.13
[BOJ] 1024번 수열의 합  (0) 2020.03.23