while
using System;
namespace codingstudy
{
class Program
{
static void Main()
{
int c = int.Parse(Console.ReadLine()); // 테스트 케이스
int[] scores;
double avg;
int cnt;
while (c --> 0)
{
// 첫번째 요소를 제외한 나머지 문자열들을 배열로 추출
scores = Array.ConvertAll(Console.ReadLine().Split()[1..],int.Parse);
// Average() 메서드로 배열의 평균 구하기
avg = scores.Average();
// Count() 메서드로 배열의 건수 구하기
// 람다식으로 scores배열에서 개별 점수가 평균보다 높은 것의 개수를 구해 Count를 호출
// cnt에는 scores 배열에서 평균보다 높은 점수의 개수가 저장됨
cnt = scores.Count(score => score > avg);
Console.WriteLine("{0:F3}%",(double)cnt / scores.Length*100);
}
}
}
}
score는 람다식의 매개변수
scores 배열의 각 요소를 하나씩 가져와서 score에 저장
score > avg 조건 테스트 후 true의 개수를 cnt에 저장
for
using System;
namespace codingstudy
{
class Program
{
static void Main()
{
int c = int.Parse(Console.ReadLine());
int[] scores;
double avg;
int cnt;
for (int i = 0; i < c; i++)
{
scores = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
avg = 0;
cnt = 0;
for (int j = 1; j < scores.Length; j++)
{
avg += scores[j];
}
avg /= scores.Length - 1;
for (int j = 1; j < scores.Length; j++)
{
if (scores[j] > avg)
{
cnt++;
}
}
Console.WriteLine("{0:F3}%", (double)cnt / (scores.Length - 1) * 100);
}
}
}
}
'C# > C# (백준)' 카테고리의 다른 글
[C#] 백준 알고리즘 11654번, 아스키코드 (0) | 2023.02.15 |
---|---|
[C#] 백준 알고리즘 4673번, 셀프 넘버 (0) | 2023.02.10 |
[C#] 백준 알고리즘 5597번, 과제 안 내신 분..? (0) | 2023.02.01 |
[C#] 백준 알고리즘 10807번, 개수 세기 (0) | 2023.01.31 |
[C#] 백준 알고리즘 10951번, A+B - 4 (0) | 2023.01.12 |