개발일지

[백준 JAVA] 10807번: 개수 세기 / 10871번: X보다 작은 수 / 10818번: 최소, 최대 / 2562번: 최댓값 /5597번: 과제 안 내신 분...? 본문

Java/알고리즘 공부

[백준 JAVA] 10807번: 개수 세기 / 10871번: X보다 작은 수 / 10818번: 최소, 최대 / 2562번: 최댓값 /5597번: 과제 안 내신 분...?

O'mil 2023. 1. 31. 16:22
728x90

#10807번: 개수 세기

문제


풀이
import java.util.Scanner;

public class Main_10807 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();		// N개의 정수
        int[] arr = new int[N];

        for ( int i = 0; i < N; i++) {
            int a = in.nextInt();
            arr[i] = a;
        }

        int V = in.nextInt();		// 찾을 정수
        int cnt = 0;				// 같은 정수의 개수

        for ( int j = 0; j < N; j++) {
            if ( arr[j] == V) {
                cnt++;
            }
        }
        in.close();
        System.out.println(cnt);
    }
}

- 총정수의 개수를 입력받는다.

- N개의 정수를 입력받기 위한 배열을 선언한다. (배열의 길이는 총정수의 개수(N) 개다.)

- 반복문을 통해 입력받은 수를 배열에 삽입한다.

- 반복문을 통해 배열의 j번째 숫자와 V가 같은지 판단한다.

- 만약 두 수가 같다면 cnt의 값을 올린다.

 


#10871번: X보다 작은 수

문제


풀이
import java.util.Scanner;

public class Main_10871 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int X = sc.nextInt();
        int[] arr = new int[N];
        int a;

        for ( int i = 0; i < N; i++) {
            a = sc.nextInt();
            arr[i] = a;
        }
        
        for ( int i = 0; i < N; i++) {
            
            if ( arr[i] < X) {
                System.out.print(arr[i] + " ");
            }
        }
        sc.close();
        System.out.println();
    }
}

- 배열에 들어온 수를 모두 넣어주고 조건문을 이용해 X보다 작을 수를 출력한다.

 


#10818번: 최소, 최대

문제

풀이
import java.util.Scanner;

public class Main_10818 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] arr = new int[N];

        for ( int i = 0; i < N; i++) {
            int a = sc.nextInt();
            arr[i] = a;
        }

        int max = arr[0];
        int min = arr[0];

        for ( int i = 1; i < N; i++) {
            
            if(arr[i] > max) {
                max = arr[i];
            }

            if ( arr[i] < min) {
                min = arr[i];
            }
        }
        sc.close();
        System.out.printf("%d %d\n", min, max);
    }
}

 


#2562번: 최댓값

문제

풀이
import java.util.Scanner;

public class Main_2562 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int a;
        int[] arr = new int[9];

        for (int i = 0; i < 9; i++) {
            a = sc.nextInt();
            arr[i] = a;
        }

        int max = arr[0];
        int temp = 1;

        for ( int i = 1; i < 9; i++) {
            if ( arr[i] > max) {
                max = arr[i];
                temp = i + 1;
            }
        }
        sc.close();
        System.out.println(max);
        System.out.println(temp);

    }
}

#5597번: 과제 안 내신 분...?

문제

 


풀이
import java.util.Scanner;

public class Main_5597 {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);

        int[] student = new int[31];

        for ( int i = 1; i < 29; i++) {
            int sucess = sc.nextInt();
            student[sucess] = 1;
        }
        for ( int i = 1; i < student.length; i++) {
            if ( student[i] != 1) {
                System.out.println(i);
            }
        }
        sc.close();
    }
}

- 배열을 선언한다.

- 반복문을 통해 28명의 과제를 제출한 학생을 입력받는다.

제출한 학생들은 각자의 자리 (배열에서 학생의 출석번호와 같은 자리)에 1이 입력된다.

- 그 후 다시 한번 반복문을 통해 1이 입력되지 않은 학생들을 차례로 출력한다.

 

 

 

 


참고)

https://velog.io/@chamominedev/백준-Baekjoon-5597번-과제-안-내신-분..-JAVA

728x90
Comments