이 문제에서는 소수점자리를 두 번째 자리까지 표현해야한다.

그렇기에 어떤 방식들이 있는지 찾아보았다. 블로그에 있는 JAVA 이론부분에 포스팅을 해놓을 예정이다.


다음은 내가 작성한 코드이다. ArrayList를 사용하였고 리스트를 정렬하는 sort를 사용해 sort 후 가장 마지막 (즉,for문을 돌려 i가 마지막으로 돌부분의 -1해준 값) 부분일 때 최대값이 저장된다고 생각하여 문제를 풀었다.


 



import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main{
public static void main(String args[]) throws NumberFormatException, IOException {
float max = 0;
float result = 0;
ArrayList<Integer> arr = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
int cnt = Integer.parseInt(sc.nextLine());
String a= sc.nextLine();
String[] b = a.split(" ");
for(int i =0; i<cnt; i++) {
arr.add(Integer.parseInt(b[i]));
if(i==cnt-1) {
Collections.sort(arr);
max=arr.get(i);
}
}
for(int i = 0;i<cnt; i++) {
float a1=Integer.parseInt(b[i]);
float x = a1/max*100;
result = result+x;
if(i==cnt-1) {
result = result/cnt;
}
}
System.out.printf("%.2f",result);
}
}
view raw test.java hosted with ❤ by GitHub

'백준 알고리즘 > JAVA' 카테고리의 다른 글

[JAVA 자바]백준 1110번  (0) 2018.07.07
[JAVA 자바] 백준 4344번  (0) 2018.07.06
[JAVA 자바] 백준 10871번  (0) 2018.07.05
[JAVA 자바] 백준 10817번  (0) 2018.07.05
[JAVA 자바] 백준 15552번  (0) 2018.07.04

+ Recent posts