본문 바로가기
Coding Test/Java Coding Test

[백준알고리즘 / 자바] - 개수 세기

by nam_ji 2024. 11. 27.

개수 세기 - 브론즈 5

문제


테스트 (인텔리제이)

package baekjoon;

import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

public class 개수_세기 {
    public static void main(String[] args) throws IOException {
        /*
        총 N개의 정수가 주어졌을 때, 정수 v가 몇 개인지 구하는 프로그램을 작성하시오.

        입력
        첫째 줄에 정수의 개수 N(1 ≤ N ≤ 100)이 주어진다.
        둘째 줄에는 정수가 공백으로 구분되어져있다.
        셋째 줄에는 찾으려고 하는 정수 v가 주어진다.
        입력으로 주어지는 정수와 v는 -100보다 크거나 같으며, 100보다 작거나 같다.

        출력
        첫째 줄에 입력으로 주어진 N개의 정수 중에 v가 몇 개인지 출력한다.

        예제 입력 1              예제 출력 1
        11                      3
        1 4 1 2 4 2 4 2 3 4 4
        2
        예제 입력 2              예제 출력 2
        11                      0
        1 4 1 2 4 2 4 2 3 4 4
        5
         */
//        solution1();
        solution2();
    }

//    public static void solution1() {
//        Scanner in = new Scanner(System.in);
//        int N = in.nextInt();
//        int[] arr = new int[N];
//        int cnt = 0;
//
//        for (int i = 0; i < N; i++) {
//            arr[i] = in.nextInt();
//        }
//        int V = in.nextInt();
//
//        for (int i : arr) {
//            if (i == V) {
//                cnt++;
//            }
//        }
//
//        System.out.println(cnt);
//
//        in.close();
//    }

    public static void solution2() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int N = Integer.parseInt(br.readLine());
        int cnt = 0;
        int[] arr = new int[N];
        StringTokenizer st = new StringTokenizer(br.readLine());

        for (int i = 0; i < N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        int V = Integer.parseInt(br.readLine());

        for (int i : arr) {
            if (i == V) {
                cnt++;
            }
        }
        System.out.println(cnt);

        br.close();
        bw.close();
        bw.close();
    }
}

백준알고리즘 코드

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
//        solution1();
        solution2();
    }

//    public static void solution1() {
//        Scanner in = new Scanner(System.in);
//        int N = in.nextInt();
//        int[] arr = new int[N];
//        int cnt = 0;
//
//        for (int i = 0; i < N; i++) {
//            arr[i] = in.nextInt();
//        }
//        int V = in.nextInt();
//
//        for (int i : arr) {
//            if (i == V) {
//                cnt++;
//            }
//        }
//
//        System.out.println(cnt);
//        
//        in.close();
//    }
    
    public static void solution2() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int N = Integer.parseInt(br.readLine());
        int cnt = 0;
        int[] arr = new int[N];
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        for (int i = 0; i < N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }
        
        int V = Integer.parseInt(br.readLine());
        
        for (int i : arr) {
            if (i == V) {
                cnt++;
            }
        }
        System.out.println(cnt);
        
        br.close();
        bw.close();
        bw.close();
    }
}