공부기록/프로그래머스

[프로그래머스] 약수의 개수와 덧셈

메델 2023. 8. 31. 23:27
import java.util.*;
class Solution {
    public int solution(int left, int right) {
        int answer = 0;

        ArrayList<Integer> numList = new ArrayList<>();
        ArrayList<Integer> list = new ArrayList<>();
                
        for(int i= left; i<=right; i++){
            int count = 0;
            numList.add(i);
            for(int j=1; j<=right;j++){
                if(i%j==0){
                    count++;
                }
            }
            list.add(count);
        }
        
        for(int i=0; i<numList.size(); i++){
            if(list.get(i)%2 == 0){
                answer+= numList.get(i);
            }else{
                answer-= numList.get(i);
            }
        }

        return answer;
    }
}