091
[C#] TQC+ 문제 CS_508: 본문
728x90
🔍C#: CS_508.
주어진 read.txt에서 각 학생의 성별로 나누어 평균을 출력하시오.
1. 문제 정답
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace CS_508{
class CS_508{
static string filePath = "read.txt";
static void Main(string[] args){
try{
int input = int.Parse(Console.ReadLine() ?? "");
if(input > 10 || input < 1) throw new Exception();
using(StreamReader reader = new StreamReader(filePath))
{
//string line;
int boy = 0,girl = 0;
int gperson = 0,bperson = 0;
for(int i = 0; i < input; i++){
string[] lines = reader.ReadLine().Split(',');
int avg = (int.Parse(lines[2]) + int.Parse(lines[3]) + int.Parse(lines[4]))/3;
if(lines[1][1] == '1'){
boy += avg;
bperson++;
} else if(lines[1][1] == '2'){
girl += avg;
gperson++;
}
}
Console.Write($"boys:{boy/(bperson==0?1:bperson)},girls:{girl/(gperson==0?1:gperson)}");
}
} catch{
Console.Write("error");
}
}
}
}
2. 정리
- 각 학생 별 평균을 구하고 더해서 남/여성의 평균의 평균값을 구해줍니다.
728x90
'Programming Language > C#' 카테고리의 다른 글
[C#] TQC+ 문제 CS_510: (0) | 2025.05.27 |
---|---|
[C#] TQC+ 문제 CS_509: (0) | 2025.05.26 |
[C#] TQC+ 문제 CS_507: (0) | 2025.05.26 |
[C#] TQC+ 문제 CS_506: (0) | 2025.05.26 |
[C#] TQC+ 문제 CS_505: (0) | 2025.05.26 |