๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[SW Expert Academy] - (D1)2071. ํ‰๊ท ๊ฐ’ ๊ตฌํ•˜๊ธฐ(Stream)

by ์ฃผ๋ฐœ2 2020. 8. 6.
๋ฐ˜์‘ํ˜•

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5QRnJqA5cDFAUq&categoryId=AV5QRnJqA5cDFAUq&categoryType=CODE

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

์ฝ”๋“œ

import java.util.Arrays;
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int T = scan.nextInt();
        
        for(int tc=1; tc<=T; tc++) {
            int[] arr = new int[10];
            long avg;
            
            for(int i=0; i<arr.length; i++) {
                arr[i] = scan.nextInt();
            }
            
            avg = Math.round(Arrays.stream(arr).average().getAsDouble());
            System.out.println("#" + tc + " " + avg);
        }
        scan.close();
    }

}

ํ’€์ด

Stream์˜ average().getAsDouble() ๋ฅผ ํ†ตํ•ด Double๋กœ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค.

 

๊ทธ ํ›„ Math.round() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์†Œ์ˆ˜์ ์€ ๋ฐ˜์˜ฌ๋ฆผ ํ•ด์„œ ๋‚˜ํƒ€๋‚ธ๋‹ค.

  - round() ๋ฆฌํ„ด ์ž๋ฃŒํ˜•: double, long

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€