Computer/Coding Test
백준 1546: 평균 (자료형 구분)
안녕도라
2023. 9. 8. 11:07
using System;
using System.Collections.Generic;
namespace CodingTestForBaekJoon
{
class Program
{
static void Main(string[] args)
{
//1: 시험 본 과목 개수 N
//2. 현재 성적 (N개의 성적 중 최댓값 M)
int N = int.Parse(Console.ReadLine());
string[] scores = Console.ReadLine().Split();
float[] scoreNum = new float[N];
for(int i = 0; i < scores.Length; i++)
{
scoreNum[i] = float.Parse(scores[i]);
}
//최댓값 구하기
float M = 0;
for (int i = 0; i < N; i++)
{
if (scoreNum[i] > M)
{
M = scoreNum[i];
}
}
float[] scoreNumAdj = new float[N];
float sum = 0;
//점수 뻥튀기 식: 점수/M *100
//평균내기
for (int i = 0; i < scoreNum.Length; i++)
{
scoreNumAdj[i] = (scoreNum[i] / M) * 100;
}
for (int i = 0; i < scoreNumAdj.Length; i++)
{
sum += scoreNumAdj[i];
}
//Console.WriteLine("첫번째 점수는" + scoreNum[0]);
//Console.WriteLine("보정점수 0번째는" + scoreNumAdj[0]);
//Console.WriteLine("최댓값은" + M);
//Console.WriteLine("sum은" + sum);
//평균 구하기
float average = sum / N;
Console.WriteLine(average);
}
}
}
오답 이유) int형과 float형의 구분 제대로 하지 않아서, 나눗셈 후 값이 0이 되어버리는 결과가 있었음