반응형
SMALL
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace BOJ2108
{
internal class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int N = int.Parse(sr.ReadLine());
int[] nums = new int[N];
for (int i = 0; i < N; i++) { nums[i] = int.Parse(sr.ReadLine()); }
//산술평균 구하기
int sum = 0;
for (int i = 0; i < N; i++) { sum += nums[i]; }
sw.WriteLine("{0}",(int)Math.Round((double)sum / N));
Array.Sort(nums);
// 중앙값 구하기
int median;
int mid = nums.Length / 2;
median = nums[mid]; // 중앙값 = 가운데 값
sw.WriteLine(median); // 중앙값 출력
//최빈값 구하기
List<int> compareList = new List<int>();
List<int> dubpleList = new List<int>();
int[] count = new int[8001];
for (int i = 0; i < nums.Length; i++) count[4000 + nums[i]]++;
compareList = count.ToList();
compareList.Sort();
int maxCount = compareList[compareList.Count-1];
for (int i = 0; i < 8001; i++)
{
if (maxCount == count[i])
{
dubpleList.Add(i - 4000);
}
}
if (dubpleList.Count == 1)
{
sw.WriteLine(dubpleList[0]);
}
else if(dubpleList.Count != 1)
{
dubpleList.Sort();
sw.WriteLine(dubpleList[1]);
}
//범위 구하기
sw.WriteLine(nums[nums.Length - 1] - nums[0]);
sr.Close();
sw.Close();
}
}
}
이전에 한번 도전했다 틀렸던 문제
이전에는 최빈값을 잘못구했었다. 이번에는 중복되는 값이 2개 이상일때도 고려하여 출력하게 해주었는데....
이상하게 계속 틀려 다른데 문제가 있나 보니 웃기게도 산술 평균을 구하는걸 잘못하고 있었다 ㅎㅎ;;;
소숫점 첫째 자리에서 반올림 해야하는데 이런 단순한 부분에서 놓쳐서 잘못 풀었다...
아무튼 이번에는 성공했으니 만족!
반응형
LIST
'Algorithm > BOJ' 카테고리의 다른 글
[BOJ] 11382번 꼬마정민 파이썬 사용 풀이 (0) | 2023.03.24 |
---|---|
[BOJ] 4375번 "1" C# 풀이 -실패 (0) | 2023.03.24 |
[BOJ] 1260번 DFS와 BFS (0) | 2023.03.05 |
[BOJ] 1940번 주몽 C# 사용 풀이 (두포인터 알고리즘 ) (0) | 2023.02.20 |
[BOJ] 2003번 수들의 합 2 C#사용 풀이 (0) | 2023.02.17 |