SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
1. 코드
import java.util.*;
public class Solution {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 1; i <= T; i++) {
int[] arr = new int[10];
int sum = 0;
for (int j = 0; j < 10; j++) {
arr[j] = sc.nextInt();
}
Arrays.sort(arr);
for (int j = 1; j < 9; j++) {
sum +=arr[j];
}
double ss = sum/8.0;
System.out.printf("#%d %d\n", i, Math.round(ss));
}
}
}
2. 배운 점
(1) Math.round(), Math.ceil(), Math.floor()
Math.round()는 ()안의 값을 소수점 첫째 자리에서 반올림해서 정수로 나타내준다. (타입: float,double => long)
이는 소수점 N번째 자리에서 반올림도 표현이 가능하다.
만약 소수 두번째 자리에서 반올림 한 값을 원한다면, Math.round(A*10)/10.0을 하면 된다.
A라는 double 형 변수를 *10 해서 반올림되는 부분을 원래 수의 소수 두 번째 자리로 맞춰준 뒤, 다시 10.0으로 나눠주면 원하는 값이 반환된다.
long/double이 되서 double 형 변수가 반환된다.
Math.ceil()은 소수점 첫째 자리에서 올림 해버리는 함수이고, double 형을 반환한다.
Math.floor()는 소수점 첫째 자리에서 내림 하는 함수이고, double 형을 반환한다.
(2) 배열을 오름차순으로 정렬 내림차순으로 정렬 (Arrays.sort 이용)
int 배열 이름이 arr이고 안에 값들이 무질서하게 들어가 있다면 sort를 이용해 크기 순으로 정렬할 수 있다.
Arrays.sort(arr)를 하면 arr 안의 원소들이 오름차순으로 정렬되어 저장된다.(index가 다 바뀐다.)
내림차순 정렬은
Arrays.sort(arr, Collections.reverseOrder())를 쓰면 된다.