본문 바로가기

프로그래머스/lv1

음양 더하기

/* 음양 더하기
 * 이 정수들의 절댓값을 차례대로 담은 정수 배열 absolutes와
 * 이 정수들의 부호를 차례대로 담은 불리언 배열 signs가 매개변수로 주어집니다.
 * 실제 정수들의 합을 구하여 return
 *
 * absolutes의 길이는 1 이상 1,000 이하입니다
 * signs의 길이는 absolutes의 길이와 같습니다
 *
 * absolutes    signs               result
 * [4,7,12]     [true,false,true]   9
 * [1,2,3]      [false,false,true]  0
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class programmer_1_25 {
    static int[] a1 = {4,7,12}; static boolean[] b1 = {true,false,true};
    static int[] a2 = {1,2,3};  static boolean[] b2 = {false,false,true};
    public int solution(int[] absolutes, boolean[] signs) {
        int answer = 0;
        int val = 0;
        for(int i = 0; i < absolutes.length; i++){
            val = signs[i] == true ? absolutes[i] : -absolutes[i];
            answer += val;
        }
        return answer;
    }
    public static void main(String args[]){
        programmer_1_25 t = new programmer_1_25();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1,b1));
        System.out.println("---------------------------------------");
        // System.out.println("result2 = " + t.solution(a2,b1));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv1' 카테고리의 다른 글

가운데 글자 가져오기  (0) 2022.12.14
없는 숫자 더하기  (0) 2022.12.14
제일 작은 수 제거하기  (0) 2022.12.14
나누어 떨어지는 숫자 배열  (0) 2022.12.14
핸드폰 번호 가리기  (0) 2022.12.14