λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Algorithm

[SW Expert Academy] - (D1)2072. ν™€μˆ˜λ§Œ λ”ν•˜κΈ°(Stream)

by 주발2 2020. 8. 5.
λ°˜μ‘ν˜•

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

 

SW Expert Academy

SW ν”„λ‘œκ·Έλž˜λ° μ—­λŸ‰ 강화에 도움이 λ˜λŠ” λ‹€μ–‘ν•œ ν•™μŠ΅ 컨텐츠λ₯Ό ν™•μΈν•˜μ„Έμš”!

swexpertacademy.com

 

 

Q) 10개의 수λ₯Ό μž…λ ₯λ°›μ•„ κ·Έ μ€‘μ—μ„œ ν™€μˆ˜λ§Œ λ”ν•œ 값을 좜λ ₯ν•˜λΌ.

 

μ½”λ“œ

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];
            
            for(int i=0; i<arr.length; i++) {
                arr[i] = scan.nextInt();
            }
            
            int sum = Arrays.stream(arr).filter(i -> i%2 == 1).sum();
            System.out.println("#" + tc + " " + sum);
        }
        
        scan.close();
    }

}

풀이

Stream의 filter() 쀑간연산을 톡해 ν™€μˆ˜λ₯Ό μΆ”μΆœν•˜κ³ , sum() λ©”μ„œλ“œλ₯Ό 톡해 합을 κ΅¬ν•œλ‹€.

 

λ°˜μ‘ν˜•

λŒ“κΈ€