반응형
SMALL
문제
코드
using System;
using System.Collections.Generic;
using System.IO;
namespace BOJ2108
{
internal class Program
{
static int[] count = new int[8001];
static void Main(string[] args)
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int N = int.Parse(sr.ReadLine());
int[] numList = new int[N];
float sum = 0;
for (int i = 0; i < N; i++) { numList[i] = int.Parse(sr.ReadLine()); }
//산술평균 구하기
for (int i = 0; i < N; i++) { sum += numList[i]; }
sum /= N;
sw.WriteLine(Math.Round(sum));
// 중앙값 구하기
Array.Sort(numList);
sw.WriteLine(numList[(int)Math.Truncate((double)(N / 2))]);
//최빈값 구하기
bool isDuplication = false;
List<int> dubpleList = new List<int>();
List<int> singNum = new List<int>();
for (int i = 0; i < numList.Length; i++) count[4000 + numList[i]]++;
for (int i = 0; i < 8001; i++)
{
if (1 <= count[i])
{
if (count[i] >= 2)
{
isDuplication = true;
dubpleList.Add(i - 4000);
}
singNum.Add(i - 4000);
}
}
if (isDuplication)
{
if (dubpleList.Count == 1) sw.WriteLine(dubpleList[0]);
else
sw.WriteLine(dubpleList[1]);
}
else
{
if (singNum.Count == 1) sw.WriteLine(singNum[0]);
else
sw.WriteLine(singNum[1]);
}
//범위 구하기
sw.WriteLine(numList[numList.Length - 1] - numList[0]);
sr.Close();
sw.Close();
}
}
}
결과창
or
or
예제들을 확인했을때는 전부 문제가 없이 잘 출력되었는데 정확히 어느 부분에서 문제가 되는지를 모르겠다;;;
최빈값을 구하는게 제일 어려웠는데 음수가 들어가다보니 음수 예외 처리 하는 부분에서 실수를 했나... 싶기도 하고..
어젯밤부터 머리 싸매고 작성하다보니 탈진해서 다른거 먼저 해야겠다...
반응형
LIST
'Algorithm > BOJ' 카테고리의 다른 글
[BOJ] 오등큰수 C# 사용 풀이 (복습 대상) (0) | 2023.02.10 |
---|---|
[BOJ] 약수 2501번 C# 사용 풀이 (0) | 2023.02.09 |
[BOJ] 1297번 TV 크기 C# 사용 풀이 (0) | 2023.02.08 |
[BOJ] 2581번 소수 C# 사용 풀이 (0) | 2023.02.06 |
[BOJ] 2605번 줄세우기 C# 사용 풀이 (0) | 2023.02.05 |