๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (์†Œ์ˆ˜ ๋งŒ๋“ค๊ธฐ, Summer/Winter Coding(~2018)) C++

by UKHYUN22 2021. 12. 30.
728x90

 

์‚ผ์ค‘ ๋ฃจํ”„๋ฅผ ๋Œ๋ ธ๋‹ค๋ผ๋Š” ๊ฒƒ ๋ง๊ณ ๋Š” ์–‘์‹ฌ์— ์ฐ”๋ฆฌ๋Š” ๋ถ€๋ถ„์ด ์•„๋‹Œ ๋ฌธ์ œ. ๋ณ„๋กœ ์„ค๋ช…ํ•  ๊ฒƒ์ด ์—†์–ด ๋ณด์ธ๋‹ค.

 

#include <vector>
#include <iostream>
using namespace std;

bool pass(int sum) {
    int count = 0;
    
    for(int i = 1 ; i <= sum ; i++) {
        if(sum % i == 0) count++;
    }
    
    if(count == 2) return true;
    else return false;
}

int solution(vector<int> nums) {
    int answer = 0;
    int sum = 0;
    
    for(int i = 0 ; i < nums.size()-2 ; i++) {
        for(int j = i+1 ; j < nums.size()-1 ; j++) {
            for(int k = j+1 ; k < nums.size() ; k++) {
                sum = nums[i] + nums[j] + nums[k];
                if(pass(sum)) answer++;
            }
        }
    }

    return answer;
}