[ 문제 풀이 날짜 / 암기 후 문제 풀이 시간 ]
1차 : 2020-10-14 (수) / 8분 30초
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
|
package 셀프테스트;
/*
* # 석차 출력
* 문제) 성적 순으로 이름 출력
*/
public class 단계4_석차출력 {
public static void main(String[] args) {
String[] name = { "홍길동", "김영", "자바킹", "민병철", "메가맨" };
int[] score = { 87, 42, 100, 11, 98 };
// 문제풀이 포인트
// 1) 처음 점수 비교 시 하나의 점수를 기준으로 세우는 for문, 그리고 그 기준을 제외한 나머지 점수와 비교하는 for문 작성하기
// 2) 점수를 비교한 후, 기준점수보다 더 높은 점수가 있을 경우 자리를 체인지 해주기 (temp 변수 이용)
for (int i = 0; i < score.length; i++) {
int maxScore = score[i];
int maxIdx = i;
for (int j = i; j < score.length; j++) {
if (maxScore < score[j]) {
maxScore = score[j];
maxIdx = j;
}
}
int scoreTemp = score[i];
score[i] = score[maxIdx];
score[maxIdx] = scoreTemp;
String nameTemp = name[i];
name[i] = name[maxIdx];
name[maxIdx] = nameTemp;
}
for (int i = 0; i < name.length; i++) {
System.out.println(name[i] + " : " + score[i]);
}
}
}
|
cs |