[스터디 배열 / Array-2]

yongcrane·2025년 3월 17일

문제 : 보이는 학생

설명

선생님이 N명의 학생을 일렬로 세웠습니다. 일렬로 서 있는 학생의 키가 앞에서부터 순서대로 주어질 때, 맨 앞에 서 있는

선생님이 볼 수 있는 학생의 수를 구하는 프로그램을 작성하세요. (앞에 서 있는 사람들보다 크면 보이고, 작거나 같으면 보이지 않습니다.)

입력

첫 줄에 정수 N(5<=N<=100,000)이 입력된다. 그 다음줄에 N명의 학생의 키가 앞에서부터 순서대로 주어진다.

출력

선생님이 볼 수 있는 최대학생수를 출력한다.

예시 입력

8
130 135 148 140 145 150 150 153

예시 출력

5

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

class Main {
    public int solution(int n, int[] arr){
       int answer = 1;
       int max = arr[0];
       
       for(int i = 1; i < n; i++){
       		if(arr[i] > max){
            	answer++;
                max = arr[i];
            }
       
       }
       
       
       return answer;
    }

    public static void main(String[] args) throws IOException {
        Main T = new Main();
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] a = new int[N];
        for (int i = 0; i < N; i++)
            a[i] = sc.nextInt();

        System.out.print(T.solution(N, a));
    }
}

문제 : 소수(에라토스테네스 체)

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

class Main {
    public int solution(int n){
        int answer = 0;
        int[] ch = new int[n+1];
        System.out.println(Arrays.toString(ch));
        for (int i = 2; i < n; i++) {
            if (ch[i] == 0) { //소수라는 것
                answer++;
                for (int j = i; j <= n; j = j + i) {
                    ch[j] = 1;
                }
            }
        }
        return answer;
    }

    public static void main(String[] args) throws IOException {
        Main T = new Main();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(T.solution(n));
    }
}

문제 : 뒤집은 소수

설명

N개의 자연수가 입력되면 각 자연수를 뒤집은 후 그 뒤집은 수가 소수이면 그 소수를 출력하는 프로그램을 작성하세요.

예를 들어 32를 뒤집으면 23이고, 23은 소수이다. 그러면 23을 출력한다. 단 910를 뒤집으면 19로 숫자화 해야 한다.

첫 자리부터의 연속된 0은 무시한다.

입력

첫 줄에 자연수의 개수 N(3<=N<=100)이 주어지고, 그 다음 줄에 N개의 자연수가 주어진다.

각 자연수의 크기는 100,000를 넘지 않는다.

출력

첫 줄에 뒤집은 소수를 출력합니다. 출력순서는 입력된 순서대로 출력합니다.

예시 입력

9
32 55 62 20 250 370 200 30 100

예시 출력

23 2 73 2 3



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

class Main {
    public boolean isPrime(int num){
        if(num==1)  return false;
        for (int i = 2; i < num; i++) {
            if(num%i==0) return false;
        }
        return true;
    }


    public ArrayList<Integer> solution(int n, int[] arr){
        ArrayList<Integer> answer = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int tmp = arr[i];
            int res =0;
            while (tmp >0){
                int t = tmp%10;
                res= res*10+t;
                tmp = tmp/10;
            }
            if(isPrime(res)) answer.add(res);
        }
        return answer;
    }

    public static void main(String[] args) throws IOException {
        Main T = new Main();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        for (int x : T.solution(n,arr) ) {
            System.out.print(x+ " ");
        }
    }
}
profile
짧고 강력하게!

0개의 댓글