본문 바로가기

C#

[백준] 10817번 세 수 C# 풀이

세 정수 중에서 두 번째로 큰 정수를 출력하는 프로그램을 작성하는 문제이다.

 

입력받은 값을 string 배열에 넣고, 다시 int 배열로 만들어

int 배열을 내림차순으로 정렬해 for문으로 반복했다.

반복문에서는 두번째로 큰 값만을 출력하도록 작성했다.

 

기초 단계 if문이라 이용해서 값을 비교하며 풀어야 할 것 같긴한데.. LINQ로 했다 

 

 

using System;
using System.Linq;
public class MainClass {
  public static void Main() {
    string value = Console.ReadLine();
    string[] strResult = value.Split(' ');
    int[] result = new int[strResult.Length];
    
    for (int i=0;i<strResult.Length;i++)
    {
      result[i] = Convert.ToInt32(strResult[i]);
    }
    
    result = result.OrderByDescending(c => c).ToArray();
    
    for(int i=0;i<result.Length;i++)
    {
      if (i==1)
         Console.WriteLine(result[i]);
    }
    
  }
}
cs