[JAVA] 백준 4단계

2024. 9. 5. 22:47코딩 테스트(Coding Test)/백준

10807 개수 세기

나의 답) 100ms

import java.io.*;
import java.util.StringTokenizer;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        int count = 0;
        int N = Integer.parseInt(br.readLine());
        
        String[] arr = new String[N];
        st = new StringTokenizer(br.readLine());
        String v = br.readLine();
        
        for (int i = 0; i < arr.length; i++) {
            arr[i] = st.nextToken();
            if (arr[i].equals(v)) count++;
        }
        System.out.print(count);
    }
}

 


10871 X보다 작은 수

나의 답) 136ms

import java.io.*;
import java.util.StringTokenizer;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        StringBuilder sb = new StringBuilder();

        int N = Integer.parseInt(st.nextToken());
        int X = Integer.parseInt(st.nextToken());

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            int temp = Integer.parseInt(st.nextToken());
            if (X > temp) sb.append(temp).append(" ");
        }
        System.out.print(sb);
    }
}

10818 최소, 최대

나의 답) 468ms

import java.io.*;
import java.util.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        StringBuilder sb = new StringBuilder();
        
        int N = Integer.parseInt(br.readLine());
        int[] arr = new int[N];
        
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }
        sb.append(Arrays.stream(arr).min().getAsInt()).append(" ").append(Arrays.stream(arr).max().getAsInt());
        System.out.print(sb);
    }
}

 

다른 사람의 답)

interface Main {
    int InputSize=9000012;
    int[]index={0};
    byte[] inputBuffer = new byte[InputSize+1];;

    static void main(String[] args) throws Exception {
         System.in.read(inputBuffer);

        int Num;
        int N = read();

        int max = -1000001;
        int min = 1000001;
        for(int i=0;i<N;i++)
        {
            Num=read();
            max=Math.max(max, Num);
            min=Math.min(min, Num);
        }
        System.out.println(min+" "+max);
    }
    //아스키코드로 숫자를 하나하나 입력받아서 정수 하나로 만들어서 반환하는 함수
    static int read(){
        int ASCII, result=0, isMinus=1;
        while((ASCII=inputBuffer[index[0]++])>32){
            //줄바꿈, 탭 등의 공백문자를 만날때까지
            if(ASCII==45){//"-"문자인지 확인, 음수인지 확인
                isMinus=-1;
            }else result=result*10+ASCII-48;
        //ASCII코드에서 0은 48이다.48을 빼면 원래 숫자가 된다.
        //result*10은 이전까지 읽어들인 숫자를 한자리 수 뒤로 밀어준다.
        //그 후 +ASCII-48로 새로 읽은 숫자를 더한다.
        }
        return result*isMinus;
    }
}

2562 최댓값

나의 답) 104ms

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int max = 0;
        int idx = 0;
        
        for (int i = 1; i <= 9; i++) {
            int temp = Integer.parseInt(br.readLine());
            if (max < temp) {
                max = temp;
                idx = i;
            }
        }
        System.out.printf("%d\n%d", max, idx);
    }
}

10810 공 넣기

나의 답) 100ms

import java.io.*;
import java.util.StringTokenizer;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        
        int[] arr = new int[N];
        for (int i = 1; i <= M; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            int z = Integer.parseInt(st.nextToken());
            
            for (int j = (x - 1); j < y; j++) {
                arr[j] = z;
            }
        }
        
        for (int i = 0; i < N; i++) {
            sb.append(arr[i]).append(" ");
        }
        System.out.print(sb);
    }
}

 

다른 사람의 답) 100ms

import java.io.*;
import java.util.StringTokenizer;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        
        int[] arr = new int[N];
        for (int i = 1; i <= M; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            int z = Integer.parseInt(st.nextToken());
            
            for (int j = (x - 1); j < y; j++) {
                arr[j] = z;
            }
        }
        
        for (int a : arr) {
            sb.append(a).append(" ");
        }
        System.out.print(sb);
    }
}

10813 공 바꾸기

나의 답) 100ms

import java.io.*;
import java.util.StringTokenizer;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = i + 1;
        }

        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            int temp;

            temp = arr[x - 1];
            arr[x - 1] = arr[y - 1];
            arr[y - 1] = temp;
        }

        for (int a : arr) {
            sb.append(a).append(" ");
        }
        System.out.print(sb);
    }
}

5597 과제 안 내신 분..?

나의 답) 100ms

import java.io.*;
import java.util.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String[] arr = new String[30];
        
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.toString(i + 1);
        }
        
        List<String> list = new ArrayList<String>();
        list.addAll(Arrays.asList(arr));
        for (int i = 0; i < 28; i++) {
            String temp = br.readLine();
            list.remove(temp);
        }
        
        for (String str : list) {
            sb.append(str).append(" ");
        }
        System.out.print(sb);
    }
}

 

다른 사람의 답1) 100ms

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        boolean[] arr = new boolean[31];
        
        for (int i = 0; i < 28; i++) {
            int n = Integer.parseInt(br.readLine());
            arr[n] = true;
        }
        for (int i = 1; i <= 30; i++) {
            if (!arr[i]) System.out.println(i);  
        }
    }
}

 

다른 사람의 답2) 96ms

import java.io.*;
import java.util.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] arr = new int[28];
        int first;
        int second;
        
        for (int i = 0; i < 28; i++) {
            arr[i] = Integer.parseInt(br.readLine());
        }
        Arrays.sort(arr);
        
        int count = 0;
        for (int i = 0; i < 28; i++) {
            if (count == 0) {
                if (arr[i] != i + 1) {
                    count++;
                    System.out.println(i + 1);
                }
            } else if (count == 1) {
                if (arr[i] != i + 2) {
                    count++;
                    System.out.println(i + 2);
                }
            }
        }
        
        if (count == 0) System.out.print("29\n30");
        else if (count == 1) System.out.print("30");
    }
}

 

다른 사람의 답3) 96ms

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] arr = new int[30];
        
        for (int i = 0; i < 28; i++) {
            int n = Integer.parseInt(br.readLine());
            arr[n - 1] = 1;
        }
        
        for (int i = 0; i < 30; i++) {
            if (arr[i] == 0) System.out.println(i + 1);
        }
    }
}

3052 나머지

나의 답) 104ms

import java.io.*;
import java.util.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] arr = new int[10];
        
        for (int i = 0; i < 10; i++) {
            arr[i] = Integer.parseInt(br.readLine()) % 42;
        }
        
        arr = Arrays.stream(arr).distinct().toArray();
        System.out.print(arr.length);
    }
}

 

다른 사람의 답) 108ms

import java.io.*;
import java.util.HashSet;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        HashSet<Integer> set = new HashSet<>();
        
        for (int i = 0; i < 10; i++) {
            int n = Integer.parseInt(br.readLine()) % 42;
            set.add(n);
        }
        System.out.print(set.size());
    }
}

10811 바구니 뒤집기

나의 답) 108ms

import java.io.*;
import java.util.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        
        int[] arr = new int[N];
        for (int i = 1; i <= N; i++) {
            arr[i - 1] = i;
        }
        
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            
            reverse(arr, x - 1, y - 1);
        }
        
        for (int a : arr) {
            sb.append(a).append(" ");
        }
        System.out.print(sb.toString().trim());
    }
    
    public static void reverse(int[] arr, int start, int end) {
        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }
}

1546 평균

나의 답) 104ms

import java.io.*;
import java.util.StringTokenizer;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        double max = 0;
        double total = 0;
        int N = Integer.parseInt(br.readLine());

        double[] arr = new double[N];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
            if (arr[i] > max) max = arr[i];
        }

        for (int i = 0; i < N; i++) {
            arr[i] = arr[i] / max * 100;
            total += arr[i];
        }
        System.out.print(total / N);
    }
}

 

다른 사람의 답) 108ms

import java.io.*;
import java.util.StringTokenizer;
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 max = 0;
        double sum = 0.0;
        
        for (int i = 0; i < N; i++) {
            int n = Integer.parseInt(st.nextToken());
            if(n > max) max = n;
            sum += n;
        }
        System.out.print(sum / max * 100.0 / N);
    }
}

'코딩 테스트(Coding Test) > 백준' 카테고리의 다른 글

[JAVA] 백준 6단계  (1) 2024.09.09
[JAVA] 백준 5단계  (3) 2024.09.09
[JAVA] 백준 3단계  (0) 2024.09.04
[JAVA] 백준 2단계  (0) 2024.09.03
[JAVA] 백준 1단계  (0) 2024.09.03