일주일에 알고리즘 두개풀기 -> 두개이상 풀기!
알고리즘 공부를 시작한지 초반이라 난이도가 낮은 문제를 하다보니 심심해서 한 두개 더풀어보다가 두개이상!으로 조건을 바꿨다. 앞으로 2개 말고도 추가로 더 올려볼 생각이다.
<문제>
![[프로그래머스] 모의고사 [프로그래머스] 모의고사](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
<작성코드>
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
|
package com.java.programers;
import java.util.Arrays;
public class Solution8 { // 모의고사(완전탐색)
public static void main(String[] args) {
int[] sol = { 1, 2, 3, 2, 2, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2, 4, 2, 3, 2, 3, 1, 2, 3, 2, 2, 2, 2, 2, 2, 2 }; // 1~10000문제
System.out.println(Arrays.toString(solution(sol)));
}
public static int[] solution(int[] answers) {
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
int[] stu1 = { 1, 2, 3, 4, 5 }; // 수포자1
int[] stu2 = { 2, 1, 2, 3, 2, 4, 2, 5 }; // 수포자2
int[] stu3 = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 }; // 수포자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[] { 1, 2, 3 };
} 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[] { 1, 2 };
} else if (cnt2 == cnt3 && cnt3 > cnt1) {
answer = new int[] { 2, 3 };
} else if (cnt1 == cnt3 && cnt3 > cnt2) {
answer = new int[] { 1, 3 };
}
return answer;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
<실행결과>
![[프로그래머스] 모의고사 [프로그래머스] 모의고사](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
![[프로그래머스] 모의고사 [프로그래머스] 모의고사](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
문제를 풀면서 배열 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언어 형식
'알고리즘' 카테고리의 다른 글
[프로그래머스] 124 나라의 숫자 (2) | 2019.05.12 |
---|---|
[프로그래머스] 예산 (1) | 2019.05.05 |
[프로그래머스] 같은 숫자는 싫어~ (0) | 2019.04.27 |
[프로그래머스] 콜라츠 추측 (0) | 2019.04.26 |
[프로그래머스] 시저암호 (2) | 2019.04.23 |
댓글