Server
[Java] Ranking 알고리즘
니용
2022. 4. 18. 20:09
반응형
구글링을 해보니 대부분 List의 알고리즘은 없고, Array에서 등수를 구하는 내용만 없어 직접 작성해보았습니다.
public static void main(String[] args) {
List<Integer> temp = List.of(10, 20, 30, 40, 15, 25, 35, 60, 70, 35);
List<Integer> scores = new ArrayList<>();
scores.addAll(temp);
Integer add = 50;
scores.add(add);
int[] rank = new int[scores.size()];
for (int i = 0; i < scores.size(); i++) {
rank[i] = 1;
}
for (int i = 0; i < scores.size(); i++) {
for (int j = 0; j < scores.size(); j++) {
if (scores.get(i) < scores.get(j)) {
rank[i]++;
}
}
}
for(int i=0; i< rank.length; i++) {
System.out.println(scores.get(i) + " >>> " + rank[i]);
}
System.out.println(rank[scores.size()-1]);
}
결과
10 >>> 11
20 >>> 9
30 >>> 7
40 >>> 4
15 >>> 10
25 >>> 8
35 >>> 5
60 >>> 2
70 >>> 1
35 >>> 5
50 >>> 3
3
반응형