C#/C# (백준)
[C#] 백준 알고리즘 1526번, 평균
서니션
2022. 8. 22. 18:08
using System;
namespace 백준1546번_평균
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
string[] s = Console.ReadLine().Split();
float[] score = Array.ConvertAll(s, float.Parse);
float max = float.MinValue;
for (int i = 0; i<n; i++)
{
if(score[i] > max)
{
max = score[i];
}
}
float sum = 0.0f;
for (int i=0; i<n; i++)
{
score[i] = score[i]/max*100;
sum += score[i];
}
Console.WriteLine("{0:0.00####}", sum/n);
}
}
}
Array.ConvertAll( 변환할 배열, 변환할 형식 )
을 사용해주면 배열의 모든 원소가 한번에 지정한 형식으로 변환된다.
출력시 :0.00은 무조건 그 자리까지 0을 채워서 출력 됨
:#.## 은 그자리에 출력할 숫자가 있으면 출력. 없으면 생략 된다.