๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์•ฝ์ˆ˜์˜ ํ•ฉ, C++

by candosh 2024. 11. 19.

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://school.programmers.co.kr/learn/courses/30/lessons/12928

โœ๐Ÿป ๋ฌธ์ œ ์„ค๋ช…

๐Ÿ’๐Ÿป‍โ™€๏ธ ๋‚ด ํ’€์ด

n๊ฐ’์˜ ๊ฐ’๋งŒํผ 1๋ถ€ํ„ฐ n๊นŒ์ง€ for๋ฌธ์„ ๋ฐ˜๋ณตํ•ด์„œ, n%i ๊ฐ’์ด 0์ธ ๊ฐ’๋“ค์„ sumํ•ด์ค€๋‹ค.

๐Ÿค– ์ฝ”๋“œ

#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = 0;
    int sum =0;
    
    for(int i=1; i<=n; i++){
        if(n%i == 0){
            sum += i;
        }
        else continue;
    }
    
    answer = sum;
    
    return answer;
}