https://www.acmicpc.net/problem/1431
정렬로 분류된 시리얼 번호 문제 입니다.
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1431
{
class Program : IComparer<string>
{
static void Main(string[] args)
{
int count = int.Parse(Console.ReadLine());
var serialList = new List<string>();
for (int i = 0; i < count; i++)
serialList.Add(Console.ReadLine());
serialList.Sort(new Program());
foreach (var serial in serialList)
Console.WriteLine(serial);
}
public int Compare(string x, string y)
{
if(x.Length == y.Length)
{
int xSum = 0;
int ySum = 0;
//각자리수 합 계산.
for(int i = 0; i < x.Length; i++)
{
int xTemp = 0;
int yTemp = 0;
if (int.TryParse(x.Substring(i, 1), out xTemp))
xSum += xTemp;
if (int.TryParse(y.Substring(i, 1), out yTemp))
ySum += yTemp;
}
return (xSum == ySum) ? x.CompareTo(y) : xSum.CompareTo(ySum);
}
return x.Length.CompareTo(y.Length);
}
}
}
|
정렬 기준만 정의해주면 되는 문제였습니다. 정렬기준 메서드는 Compare의 내용입니다.
정렬 후 List 출력하면 되겠습니다.
백준#3020 - 개똥벌레 (0) | 2020.05.04 |
---|---|
백준# 2959 - 거북이 (0) | 2020.05.03 |
백준#5052 - 전화번호 목록 (0) | 2020.05.02 |
백준#11652 - 카드 (0) | 2020.04.29 |
백준#3047 - ABC (0) | 2020.04.21 |