상세 컨텐츠

본문 제목

백준# 2870 - 수학숙제

C#/알고리즘

by McRobbin 2020. 5. 22. 14:58

본문

https://www.acmicpc.net/problem/2870

 

2870번: 수학숙제

문제 상근이는 수학시간에 딴 짓을 하다가 선생님께 걸렸다. 선생님은 상근이에게 이번 주말동안 반성하라며 엄청난 숙제를 내주었다. 선생님이 상근이에게 준 종이에는 숫자와 알파벳 소문자�

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _2870
{
    class Program : IComparer<string>
    {
        static void Main(string[] args)
        {
            int count = int.Parse(Console.ReadLine());
 
            var lineList = new List<string>();
            for (int i = 0; i < count; i++)
                lineList.Add(Console.ReadLine());
 
            var ansList = new List<string>();
            foreach (var line in lineList)
            {
                string strBuff = "";
                int buff = 0;
                for (int i = 0; i < line.Length; i++)
                {
                    if (int.TryParse(line.Substring(i, 1), out buff))
                        strBuff += line.Substring(i, 1);
                    else
                    {
                        if (!string.IsNullOrEmpty(strBuff))
                            ansList.Add(strBuff);
 
                        strBuff = "";
                    }
                }
 
                if (!string.IsNullOrEmpty(strBuff))
                    ansList.Add(strBuff);
            }
            for (int i = 0; i < ansList.Count; i++)
                ansList[i] = Program.DeleteZero(ansList[i]);
            ansList.Sort(new Program());
            ansList.ForEach(x => Console.WriteLine(x));
        }
 
        public int Compare(string s1, string s2)
        {
            
            if (s1.Length == s2.Length)
            {
                for(int i = 0; i < s1.Length; i++)
                {
                    if (s1[i] != s2[i])
                        return s1[i].CompareTo(s2[i]);
                }
 
                return 0;
            }
 
            else
                return s1.Length.CompareTo(s2.Length);
        }
 
        public static string DeleteZero(string s)
        {
            if(s.Length > 1 && s.First() == '0')
            {
                return DeleteZero(s.Substring(1));
            }
 
            return s;
        }
    }
}
 
cs

발견되는 숫자가 int, long을 넘어가는 경우가 있어 string으로 처리하여 정리했습니다.

자릿수가 같다면 자리를 하나씩 보며 비교했고 자릿수가 다르다면 큰것을 뒤로 보내면 됩니다.

 

앞에 불필요하게 나오는 0이 발생하는데 이걸 없애기 위해 DeleteZero함수를 정의했습니다.

가장 앞의 0을 지우고 재귀호출 했습니다.

관련글 더보기