Post

Array_등수 계산하기

✅ N명 학생 점수가 입력되었을 때 등수 출력하기. 단, 같은 점수는 같은 등수입니다.

예를 들어, 100 92 92 85 70 => 1 2 2 4 5

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
class Main {
    public int[] solution(int n, int[] intArr){
    int[] answer= new int[n];

    for(int i=0; i<n; i++){
        int count=1;
        for(int j=0; j<n; j++){
            if(intArr[i]< intArr[j]){
                count++;
            }
        }
        answer[i]= count;
    }
    return answer;

}
    public static void main(String[] args) {
        Main T = new Main();
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int[] intArr = new int[n];
        for(int i=0; i<n; i++){
            intArr[i]= sc.nextInt();
        }
        for(int x: T.solution(n, intArr)){
            System.out.print(x+ " ");
        }
    }
}



//⭐️input:
//5
//87 89 92 100 76
//⭐️output:
//4 3 2 1 5

🔵 ThingsILearned

✔️ 한 숫자를 고정해두고 다른 숫자들이랑 비교하려면 이중 for문을 사용하면 된다.

✔️ 규칙을 찾기 어려울 때면 표를 만들어서 간소화해보자

코딩공책-26

🟢

🟢

This post is licensed under CC BY 4.0 by the author.