본문 바로가기

알고리즘/백준

[백준] 1546 - 평균

반응형

1546번 문제(1)
1546번 문제(2)
1546번 문제(3)

 

1. Scanner를 이용한 방법

import java.util.*;

public class Main{
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        //시험 본 과목의 개수
        int N = sc.nextInt();
        int[] test = new int[N];
        double total = 0.0;

        //최댓값
        int max = -1000;
        
        for(int i=0; i<test.length; i++){
            test[i] = sc.nextInt();

            max = Math.max(max, test[i]); 
        }
        for(int j=0; j<test.length; j++){
            total += test[j];
        }
        System.out.println((total/max*100)/N);
         
        sc.close();
    }
}

 

 

2. BufferedReader를 이용한 방법

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main{
    public static void main(String[] args)throws IOException{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        //시험 본 과목의 개수
        int N = Integer.parseInt(br.readLine());
        int[] test = new int[N];
        double total = 0.0;

        //최댓값
        int max = -1000;
        StringTokenizer st = new StringTokenizer(br.readLine());

        for(int i=0; i<test.length; i++){
            test[i] = Integer.parseInt(st.nextToken());

            max = Math.max(max, test[i]); 
        }
        for(int j=0; j<test.length; j++){
            total += test[j];
        }
        System.out.println((total/max*100)/N);
         
        br.close();
    }
}

결과 출력에 75.0 으로 뜨기 때문에 total을 double로 지정해주어야한다. 

반응형

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

[백준] 2743 - 단어 길이 재기  (0) 2024.02.22
[백준] 27866 - 문자와 문자열  (0) 2024.02.21
[백준] 10811 - 바구니 뒤집기  (0) 2024.02.19
[백준] 3052 - 나머지  (0) 2024.02.18
[백준] 5597 - 과제 안 내신 분..?  (0) 2024.02.17