091
[C#] TQC+ 문제 CS_308 : 본문
728x90
🔍C#: CS_308 .
입력된 값이 주어진 배열 scores에 그 점수를 가진 사람의 수를 세서 반환하시오.
1. 문제 정답
using System;
using System.Collections.Generic;
using System.Security.Authentication;
namespace CS_308 {
class CS_308{
static int[] scores = new int[] { 100, 100, 95, 95, 92, 91, 90, 100, 88, 88, 87, 87, 90, 91, 85, 80, 81, 82, 82, 89 };
static void Main(string[] args){
int input = CheckNumber();
int count = 0;
foreach(int num in scores){
if(num == input) count++;
}
Console.Write(count);
}
static int CheckNumber(){
if(int.TryParse(Console.ReadLine(), out int num)){
return num;
} else return -1;
}
}
}
2.정리
- foreach를 통해 객체의 요소에 인덱스 없이도 쉽게 접근할 수 있습니다.
728x90
'Programming Language > C#' 카테고리의 다른 글
[C#] TQC+ 문제 CS_310 (0) | 2025.05.14 |
---|---|
[C#] TQC+ 문제 CS_309 : (0) | 2025.05.14 |
[C#] TQC+ 문제 CS_307 : (0) | 2025.05.14 |
[C#] TQC+ 문제 CS_306 : (0) | 2025.05.14 |
[C#] TQC+ 문제 CS_305 : (0) | 2025.05.14 |