관리 메뉴

Partially Committed

[μ›”κ°„ μ½”λ“œ μ±Œλ¦°μ§€ μ‹œμ¦Œ2] μ•½μˆ˜μ˜ κ°œμˆ˜μ™€ λ§μ…ˆ λ³Έλ¬Έ

πŸ”₯ Algorithm || λ¬Έμ œν’€μ΄/PS

[μ›”κ°„ μ½”λ“œ μ±Œλ¦°μ§€ μ‹œμ¦Œ2] μ•½μˆ˜μ˜ κ°œμˆ˜μ™€ λ§μ…ˆ

WonderJay 2022. 7. 4. 17:59
728x90
λ°˜μ‘ν˜•
SMALL

https://programmers.co.kr/learn/courses/30/lessons/77884

 

μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅ - μ•½μˆ˜μ˜ κ°œμˆ˜μ™€ λ§μ…ˆ

두 μ •μˆ˜ left와 rightκ°€ λ§€κ°œλ³€μˆ˜λ‘œ μ£Όμ–΄μ§‘λ‹ˆλ‹€. leftλΆ€ν„° rightκΉŒμ§€μ˜ λͺ¨λ“  μˆ˜λ“€ μ€‘μ—μ„œ, μ•½μˆ˜μ˜ κ°œμˆ˜κ°€ 짝수인 μˆ˜λŠ” λ”ν•˜κ³ , μ•½μˆ˜μ˜ κ°œμˆ˜κ°€ ν™€μˆ˜μΈ μˆ˜λŠ” λΊ€ 수λ₯Ό return ν•˜λ„λ‘ solution ν•¨μˆ˜λ₯Ό μ™„μ„±ν•΄μ£Ό

programmers.co.kr

 

μ•½μˆ˜μ˜ 개수λ₯Ό λ¦¬ν„΄ν•˜λŠ” get_num_of_divisor 을 μ •μ˜ν•œ λ’€, left λΆ€ν„° right κΉŒμ§€μ˜ 수λ₯Ό μˆœνšŒν•˜λ©°

get_num_of_divisor 의 홀짝 여뢀에 λ”°λΌμ„œ answer 에 λ”ν•˜κ±°λ‚˜ λΉΌλ©΄ λœλ‹€.

 

 

[C++]

#include <string>
#include <vector>

using namespace std;

int get_num_of_divisor(int x)
{
    int num_of_divisor = 0;

    for (int i = 1; i <= x; i++)
    {
        if (x % i == 0)
        {
            num_of_divisor++;
        }
    }

    return num_of_divisor;
}

int solution(int left, int right) {
    int answer = 0;

    for (int i = left; i <= right; i ++)
    {
        if (get_num_of_divisor(i) % 2 == 1)
        {
            answer -= i;
        }
        else
        {
            answer += i;
        }
    }

    return answer;
}

 

 

728x90
λ°˜μ‘ν˜•
LIST
Comments