알고리즘

[프로그래머스] 모의고사

건뱅 2019. 4. 30.
반응형

일주일에 알고리즘 두개풀기 -> 두개이상 풀기!

알고리즘 공부를 시작한지 초반이라 난이도가 낮은 문제를 하다보니 심심해서 한 두개 더풀어보다가 두개이상!으로 조건을 바꿨다. 앞으로 2개 말고도 추가로 더 올려볼 생각이다.

 

<문제>

 

 

<작성코드>

 

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
 
public class Solution8 { // 모의고사(완전탐색)
 
    public static void main(String[] args) {
        int[] sol = { 123222222252222423231232222222 }; // 1~10000문제
        System.out.println(Arrays.toString(solution(sol)));
    }
 
    public static int[] solution(int[] answers) {
 
        int cnt1 = 0, cnt2 = 0, cnt3 = 0;
        int[] stu1 = { 12345 }; // 수포자1
        int[] stu2 = { 21232425 }; // 수포자2
        int[] stu3 = { 3311224455 }; // 수포자3
        // stu1
        for (int i = 0, j = 0; i < answers.length;) {
            if (j < stu1.length) {
                if (answers[i] == stu1[j]) {
                    cnt1++;
                }
            } else if (j >= stu1.length) {
                j = j - stu1.length;
                if (answers[i] == stu1[j]) {
                    cnt1++;
                }
            }
            i++;
            j++;
        }
        // stu2
        for (int i = 0, j = 0; i < answers.length;) {
            if (j < stu2.length) {
                if (answers[i] == stu2[j]) {
                    cnt2++;
                }
            } else if (j >= stu2.length) {
                j = j - stu2.length;
                if (answers[i] == stu2[j]) {
                    cnt2++;
                }
            }
            i++;
            j++;
        }
        // stu3
        for (int i = 0, j = 0; i < answers.length;) {
            if (j < stu3.length) {
                if (answers[i] == stu3[j]) {
                    cnt3++;
                }
            } else if (j >= stu3.length) {
                j = j - stu3.length;
                if (answers[i] == stu3[j]) {
                    cnt3++;
                }
            }
            i++;
            j++;
        }
        System.out.println("수포자1은 " + cnt1 + "/" + answers.length + "개를 맞췄습니다.");
        System.out.println("수포자2은 " + cnt2 + "/" + answers.length + "개를 맞췄습니다.");
        System.out.println("수포자3은 " + cnt3 + "/" + answers.length + "개를 맞췄습니다.");
 
        int answer[] = null;
        if (cnt1 == cnt2 && cnt2 == cnt3) {
            answer = new int[] { 123 };
        } else if (cnt1 > cnt2 && cnt1 > cnt3) {
            answer = new int[] { 1 };
        } else if (cnt2 > cnt1 && cnt2 > cnt3) {
            answer = new int[] { 2 };
        } else if (cnt3 > cnt1 && cnt3 > cnt2) {
            answer = new int[] { 3 };
        } else if (cnt1 == cnt2 && cnt1 > cnt3) {
            answer = new int[] { 12 };
        } else if (cnt2 == cnt3 && cnt3 > cnt1) {
            answer = new int[] { 23 };
        } else if (cnt1 == cnt3 && cnt3 > cnt2) {
            answer = new int[] { 13 };
        }
        return answer;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

<실행결과>

 

 

 

문제를 풀면서 배열 answer를 선언할때

int answer[] = null;     //배열 선언

answer = {1, 2, 3};      //배열 할당

이런식으로 코드를 짰더니

* Array constants can only be used in initializers ( 배열상수는 초기화에 사용할 수 있습니다.) ...???

 배열을 선언하고, 그 배열에 값을 넣어준다고 생각하였는데 에러가 발생하였다.

 

나는 보통 배열을 초기화 할 때 int answer[] ={1, 2, 3, 4}; 와 같이 사용하였지만, 이는 C언어 문법으로 JAVA가 C언어 사용자를 위해 C언어 문법과 같이 쓸 수 있도록 지원하기에 가능한 것이라고 한다. 나도모르게 오류가 안나니까 C언어 문법을 계속 사용해왔던 것 같다....

 

자바는 포인터를 직접사용하지 않기 때문에 객체 선언 new를 사용하여 동적 메모리 할당 영역에 메모리를 잡는 방법을 사용한다.

int[] answer = new int[]{1, 2, 3, 4};

동적 메모리 영역을 할당 하므로 선언을 먼저하고 나중에 초기화를 할 수도 있다.

int[] answer = null;

answer = new int[]{1, 2, 3, 4};

 

** C언어와의 비교 **

배열 선언

int[] a = new int[10];      //JAVA형식!

int a[] = new int[10];      //C언어 형식

 

배열 선언과 초기화

String[] arr = new String[]{"AA", "BB", "CC"};  //JAVA 형식!

String arr[] = {"AA", "BB", "CC"};                  //C언어 형식

 

 

반응형

댓글