본문 바로가기

Java/Java 알고리즘 인프런

[Ch.02 - Array] 08. 등수 구하기 (+ Arrays.sort와 fill)

반응형
8. 등수구하기
 

설명

N명의 학생의 국어점수가 입력되면 각 학생의 등수를 입력된 순서대로 출력하는 프로그램을 작성하세요.

같은 점수가 입력될 경우 높은 등수로 동일 처리한다.

즉 가장 높은 점수가 92점인데 92점이 3명 존재하면 1등이 3명이고 그 다음 학생은 4등이 된다.

입력

첫 줄에 N(3<=N<=100)이 입력되고, 두 번째 줄에 국어점수를 의미하는 N개의 정수가 입력된다.

출력

입력된 순서대로 등수를 출력한다.

예시 입력 1 

5
87 89 92 100 76

예시 출력 1

4 3 2 1 5

*SQL에서 순위 함수

 

RANK는 동점자의 있는 경우 동일 순번을 리턴, 다음 순위는 다음 숫자를 하나를 건너뛴다. (1, 2, 2, 4, 4, 6, .. )

DENSE_RANK는 동점자의 경우에 동일한 순번을 리턴, 다음 순위는 다음 숫자부터 진행이 된다 (1, 2, 2, 3, 3, 4, .. )

ROW_NUMBER의 경우 동점자가 있는 경우에 정렬이 된 순서대로 순서를 반환한다. (1, 2, 3, 4, 5, 6, 7, ...)

 

-> 이 경우 RANK함수를 따른다.


 

import java.util.Scanner;
  
public class Main {
  public static void main(String[] args){
    Scanner kb=new Scanner(System.in);
    int n = kb.nextInt();
    int[] arr = new int[n];
    for(int i=0;i<n;i++){
      arr[i]=kb.nextInt();
    }
    for(int x:solution(n, arr))
    System.out.print(x+" ");
  }
  static int[] solution(int n, int[] arr){
    //인덱스로 값 비교 -> 이중 for문
    int[] answer=new int[n];
    for(int i=0;i<n;i++){
      int cnt=1;
      for(int j=0;j<n;j++){
        if(arr[i]<arr[j]) cnt++;
      }
      answer[i]=cnt;
    }
    return answer;
  }
}

 

import java.util.Scanner;
  
public class Main {
  public int[] solution(int n,int[] arr){

    int tmp=0;
    int [] answer = new int[n];
    for(int p1=0;p1<n;p1++){
      tmp=1;
      for(int p2=0;p2<n;p2++){
        if(p1!=p2){
       	  if(arr[p2]>arr[p1]){
            tmp++;
          	answer[p1]=tmp;
          }
          
          else {
        	answer[p1]=tmp;          
          }
        }
      }
    }
    return answer;
  }
  public static void main(String[] args){
    Main T = new Main();
    Scanner kb=new Scanner(System.in);
    int n=kb.nextInt();
    int [] arr = new int[n];
    for(int i=0;i<n;i++){
      arr[i]=kb.nextInt();
    }
    for(int x:T.solution(n,arr)){
      System.out.print(x+" ");
    }
    
   
  }
}

 

+) 맵을 이용해서 풀어보기 -> map + 정렬

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
	static class score implements Comparable<score> {
		public int index;
		public int score;

		public score(int index, int score) {
			this.index = index;
			this.score = score;

		}

		@Override
		public int compareTo(score o) {
			return o.score - this.score;
		}

	}

	public static void main(String[] args) {
		Main T = new Main();
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt();
		ArrayList<score> list = new ArrayList<>();
		for (int i = 1; i <= n; i++)
			list.add(new score(i, kb.nextInt()));

		Collections.sort(list);
		int[] answer = new int[n];
		int tmp = -1;
		for (int i = 0; i < list.size(); i++) {
			if (tmp == list.get(i).score) {
				answer[list.get(i).index - 1] = answer[list.get(i-1).index - 1];
			} else {
				answer[list.get(i).index - 1] = i + 1;
			}
			tmp = list.get(i).score;

		}
		for (int x : answer)
			System.out.print(x + " ");

	}
}

 

 

+) 세련된 코드

: 비교대상보다 클 경우, 등수를 높여서 입력

import java.util.*;

class Main {
	public int[] solution(int n, int[] arr) {
		int[] answer = new int[n];
		for (int i = 0; i < n; i++) {
			int cnt = 1;
			for (int j = 0; j < n; j++) {
				if (arr[j] > arr[i])
					cnt++;
			}
			answer[i] = cnt;
		}
		return answer;
	}

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

 

학생의 수로 배열을 초기화해서

: 비교대상 보다 같거나 클 경우,  등수를 높인다.

import java.util.*;
  
public class Main {
  public static void main(String[] args){
    Scanner in=new Scanner(System.in);
    int n = in.nextInt();
    int[] arr = new int[n];
    for(int i=0;i<n;i++){
      arr[i]=in.nextInt();
    }
    solution(n, arr);
  }
  static void solution(int n, int[]arr){
    //같은 점수일 경우 높은 등수로 동일처리, 등수를 입력한 순서대로 출력
    int[] grade = new int[n];
    Arrays.fill(grade, n);
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        if(i!=j&&arr[i]<=arr[j]) grade[j]-=1;
      }
    }
    for(int x: grade) System.out.print(x+" ");
  }
}

(1) ArrayList.sort를 이용해 등수 매기기

import java.util.*;

public class Main {
  public static void main(String[] args){
    Scanner in=new Scanner(System.in);
    int n = in.nextInt();
    int[] arr = new int[n];
    ArrayList<Integer> list = new ArrayList<>();
    for(int i=0;i<n;i++){
      arr[i]=in.nextInt();
      list.add(arr[i]);
    }
    //while()을 이용해 앞을 돈다
    Collections.sort(list, Collections.reverseOrder());
    for(int i=0;i<n;i++){
      System.out.print((list.indexOf(arr[i])+1)+" ");
    }
  }
}

 

(2) 배열만 이용해 Arrays.fill로 모든 배열에 1로 초기화 후, 이중for문으로 점수 비교해 등수매기기

import java.util.*;
  
public class Main {
  public static void main(String[] args){
    Scanner in=new Scanner(System.in);
    int n = in.nextInt();
    int[] arr= new int[n];
    for(int i=0;i<n;i++){
      arr[i]=in.nextInt();
    }
    int[] answer= new int[n];
    Arrays.fill(answer, 1);
    for(int i=0;i<n;i++){
      for(int j=0; j<n; j++){
        if(arr[j]>arr[i]) answer[i]+=1;
      }
    }
    for(int x:answer) System.out.print(x+" ");
  }
}

 

더보기

+) 동일 점수의 다음 순위는 다음 숫자부터 진행하도록 바꾸기

 

 

 

반응형