24.03.01 TIL - 알고리즘 : 백준, 개수 세기 (10807번) | Array.FindAll : 배열에서 조건에 맞는 모든 요소검색

JJwoo·2024년 3월 1일

알고리즘

목록 보기
15/18

풀이 : for 문

            int n = int.Parse(Console.ReadLine()); // 정수 개수 N
            string[] s = Console.ReadLine().Split(' '); // n개의 정수를 입력받아 배열로 저장
            int v = int.Parse(Console.ReadLine()); // 정수 v

            int cnt = 0; // v의 개수를 셀 변수
            for (int i = 0; i < n; i++)
            {
                if (int.Parse(s[i]) == v) cnt++;
            }
            Console.WriteLine(cnt);

다른 풀이 : Array.FindAll 사용

     static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());  

            string [] s = Console.ReadLine().Split();

            string v = Console.ReadLine();

            int result = Array.FindAll(numbers, x => x == v).Length;

            Console.WriteLine(result);
profile
개발 모코코

0개의 댓글