๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/๋ฐฑ์ค€

[๋ฐฑ์ค€/Java] 10807 - ๊ฐœ์ˆ˜ ์„ธ๊ธฐ (๋ฐฐ์—ด์„ ์•ˆ์ผ๋Š”๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ฆ๊ฐ€ํ•˜๋Š” ์ด์œ ๋Š”?)

by ๋ฝ€์งœ๊ผฌ 2025. 1. 19.
728x90
๋ฐ˜์‘ํ˜•

๋ฐฑ์ค€

 

๋ฌธ์ œ

์ด N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ •์ˆ˜ v๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ •์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด์ ธ์žˆ๋‹ค. ์…‹์งธ ์ค„์—๋Š” ์ฐพ์œผ๋ ค๊ณ  ํ•˜๋Š” ์ •์ˆ˜ v๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜์™€ v๋Š” -100๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉฐ, 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

11
1 4 1 2 4 2 4 2 3 4 4
2

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ N๊ฐœ์˜ ์ •์ˆ˜ ์ค‘์— v๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

3

1. ์ฒ˜์Œ ์ฝ”๋“œ (104ms)

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int v = Integer.parseInt(br.readLine());

        int[] nArray = new int[N];
        int count = 0;

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

        for (int i = 0; i < N; i++) {
            if (nArray[i] == v) {
                count++;
            }
        }
        System.out.println(count);
    }
}

 

๋ง ๊ทธ๋Œ€๋กœ for๋ฌธ์„ ์ด์šฉํ•ด ๊ฐœ์ˆ˜์ธ count๋ฅผ ์˜ฌ๋ฆฌ๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

๊ทธ๋Ÿฐ๋ฐ ์ด๊ฑฐ ๋ง๊ณ  ์ข€ ๋” ์Œˆ๋ฝ•ํ•œ๊ฒŒ ์—†๋‚˜ ํƒ๊ตฌํ•ด๋ดค๋‹ค.

 

 

2. ๋ฐฐ์—ด ์•ˆ์“ฐ๋ฉด ์•ˆ๋˜๋‚˜? => ์—ฅ ์‹œ๊ฐ„ ๋”๊ฑธ๋ฆผ

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int v = Integer.parseInt(br.readLine());

        int count = 0;
        for (int i = 0; i < N; i++) {
            if (Integer.parseInt(st.nextToken()) == v) {
                count++;
            }
        }
        System.out.println(count);
    }
}

 

๋ฐฐ์—ด์„ ๋นผ๋ฒ„๋ ธ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๋ฐฑ์ค€์—์„œ๋Š” ์ด๊ฒŒ ์‹œ๊ฐ„์€ ์ฐจ์ด๊ฐ€ ์—†๊ณ , ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋” ์ผ๋‹ค.

์™œ์ง€? ๋ฐฐ์—ด์ด ๋” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์“ธ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ.

  • ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์—์„œ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•œ ๋ฒˆ ํ• ๋‹นํ•œ ๋’ค ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— Garbage Collection(GC)์˜ ๋ถ€๋‹ด์ด ์ ๋‹ค.
  • ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ฝ”๋“œ์—์„œ๋Š” ๊ฐ ํ˜ธ์ถœ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ์ฆ‰์‹œ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๊ฐ์ฒด๊ฐ€ GC ๋Œ€์ƒ์ด ๋œ๋‹ค. ์ด๋กœ ์ธํ•ด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ฆ๊ฐ€ํ•œ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๊ณ  ํ•œ๋‹ค. ํ•œ๋ฒˆ ๋ฉ”๋ชจ๋ฆฌ ๋‚ด๊บผ์•ผ! ํ•˜๋Š”๊ฑฐ๋ž‘

์˜คํ˜ธํ™“ ์ €๊ฒƒ๋„ ๋‚ด๊บผ ์ €๊ฒƒ๋„ ๋‚ด๊บผ ํ•˜๋Š”๊ฑฐ๋ž‘์˜ ์ฐจ์ด.

 

 

3. for๋ฌธ์„ ํ•œ๋ฒˆ๋งŒ ์“ฐ๊ณ ์‹ถ์—ˆ์Œ

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int[] nArray = new int[N];

        StringTokenizer st = new StringTokenizer(br.readLine());
        int v = Integer.parseInt(br.readLine());
        int count = 0;

        for (int i = 0; i < N; i++) {
            nArray[i] = Integer.parseInt(st.nextToken());
            if (nArray[i] == v) {
                count++;
            }
        }
        System.out.println(count);
    }
}

 

4. ํ† ํฐ ์•ˆ์“ธ๋žญ (๋‹น์—ฐํ•จ. ์‹œ๊ฐ„ ๋” ๊ฑธ๋ฆผ)

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        String[] input = br.readLine().split(" ");
        int v = Integer.parseInt(br.readLine());

        int count = 0;
        for (int i = 0; i < N; i++) {
            if (Integer.parseInt(input[i]) == v) {
                count++;
            }
        }
        System.out.println(count);
    }
}

 

728x90
๋ฐ˜์‘ํ˜•