문제 출처: https://www.acmicpc.net/problem/2108



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
 
public class Main {
    
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        int[] arr = new int[N];
        int[] check = new int[8001];
        int sum = 0;
    
        for(int i=; i<N ; i++){
            arr[i] =  Integer.parseInt(reader.readLine());
            // 최빈값 중 두 번째로 작은 값을 출력하기 위해 수가 몇 번 나왔는지 배열로 카운트
            check[arr[i]+4000]++;
            sum += arr[i];
        }
        
        //  ArrayList를 사용해 여러 개의 최빈값을 저장 
        int maxIndex = 0;
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=; i<8001 ; i++){
            if(check[maxIndex] < check[i]){
                maxIndex = i;
                list.clear();
            }
            else if(check[i]!=&& check[i]==check[maxIndex]){
                list.add(i-4000);
            }
        }
        
        Arrays.sort(arr);
        
        // 소수점 이하 첫째 자리에서 반올림
        System.out.println((int)Math.round((double)sum/N));
        System.out.println(arr[N/2]);
        if(list.size()!=0)
            System.out.println(list.get(0));
        else
            System.out.println(maxIndex-4000);
        System.out.println(arr[N-1]-arr[0]);
    }
}
cs



'Algorithm' 카테고리의 다른 글

백준 1181번: 단어 정렬  (0) 2018.10.20
백준 1427번: 소트인사이드  (0) 2018.10.20
백준 10989번: 수 정렬하기 3  (0) 2018.10.20
백준 2751번: 수 정렬하기 2  (0) 2018.10.20
백준 2750번: 수 정렬하기  (0) 2018.10.20

문제 출처: https://www.acmicpc.net/problem/10989



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
 
public class Main {
    public static void main(String[] args) throws Exception {
        //solution1();
        solution2();
    }
    // 처음 풀었던 방법
    public static void solution1() throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        int[] arr = new int[N];
        
        for(int i=; i<arr.length ; i++)
            arr[i] = Integer.parseInt(reader.readLine());
        
        Arrays.sort(arr);
        
        StringBuilder builder = new StringBuilder();
        for(int i=; i<arr.length ; i++)
            builder.append(arr[i]).append("\n");
        
        System.out.println(builder);
    }
    // 더 나은 방법
    public static void solution2() throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        int[] count = new int[10000];
        // 10,000보다 작거나 같은 자연수 & 오름차순 정렬이므로,
        // 배열의 크기를 10000으로 만든 후, 중복 개수 카운트 후 출력
        for(int i=; i<N ; i++)
            count[Integer.parseInt(reader.readLine())-1]++
        
        StringBuilder builder = new StringBuilder();
        for(int i=; i<10000 ; i++)
            for(int j=; j<count[i] ; j++)
                builder.append(i+1).append("\n");
        
        System.out.println(builder);
    }
}
cs


'Algorithm' 카테고리의 다른 글

백준 1427번: 소트인사이드  (0) 2018.10.20
백준 2108번: 통계학  (1) 2018.10.20
백준 2751번: 수 정렬하기 2  (0) 2018.10.20
백준 2750번: 수 정렬하기  (0) 2018.10.20
백준 2941번: 크로아티아 알파벳  (0) 2018.10.20

문제 출처: https://www.acmicpc.net/problem/2751



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
 
public class Main {
    
    public static void main(String[] args) throws Exception {
        //solution1();
        solution2();
    }
    // 처음 풀었던 방법
    public static void solution1() throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        int[] arr = new int[N];
        
        for(int i=; i<arr.length ; i++)
            arr[i] = Integer.parseInt(reader.readLine());
        
        Arrays.sort(arr);
        
        StringBuilder builder = new StringBuilder();
        for(int i=; i<arr.length ; i++)
            builder.append(arr[i]).append("\n");
        
        System.out.println(builder);
    }
    // 더 나은 방법
    public static void solution2() throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        boolean[] check = new boolean[2000001];
        // 절대값이 1,000,000보다 작거나 같은 정수가 주어지므로, 
        // 배열을 2,000,000으로 만들어 1,000,000을 전후로 음수,양수로 나눔
        for(int i=; i<N ; i++)
            check[Integer.parseInt(reader.readLine()) + 1000000= true
        
        StringBuilder builder = new StringBuilder();
        for(int i=; i<2000001 ; i++)
            if(check[i])
                builder.append(i-1000000).append("\n");
        
        System.out.println(builder);
    }
}
cs


'Algorithm' 카테고리의 다른 글

백준 2108번: 통계학  (1) 2018.10.20
백준 10989번: 수 정렬하기 3  (0) 2018.10.20
백준 2750번: 수 정렬하기  (0) 2018.10.20
백준 2941번: 크로아티아 알파벳  (0) 2018.10.20
백준 5622번: 다이얼  (0) 2018.10.20

+ Recent posts