2017. 12. 15. 22:32
어떤 수를 입력받아 그 수의 약수를 모두 더한 수 sumDivisor 함수를 완성해 보세요. 예를 들어 12가 입력된다면 12의 약수는 [1, 2, 3, 4, 6, 12]가 되고, 총 합은 28이 되므로 28을 반환해 주면 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
int sumDivisor(int n) | |
{ | |
int result = 0; | |
for(int i =1; i <= n; i++) | |
{ | |
if(n % i == 0) | |
result += i; | |
} | |
return result; | |
} | |
int main() | |
{ | |
int testCase = 10; | |
int testAnswer = sumDivisor(testCase); | |
cout<<testAnswer; | |
} |
'Programming > C++' 카테고리의 다른 글
프로그래머스 Level 2.최솟값 만들기 (0) | 2017.12.15 |
---|---|
알고리즘 문제 예제 4 (0) | 2017.12.06 |
알고리즘 문제 예제 3 (0) | 2017.12.06 |
알고리즘 문제 예제 2 (0) | 2017.12.06 |
알고리즘 문제 예제 1 (0) | 2017.12.06 |