1부터 n까지 숫자를 돌며 숫자를 더해가면서 합이 n이 되는 경우를 count해준다.
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int solution(int n) {
int answer = 0;
for(int i=1; i<=n; i++) {
int sum = 0;
int n_inc = i;
while(sum < n) {
sum += n_inc;
if(sum==n) {
answer++;
break;
}
n_inc++;
}
}
return answer;
}
'프로그래머스' 카테고리의 다른 글
| 프로그래머스 N개의 최소공배수, c++ (0) | 2024.02.22 |
|---|---|
| 프로그래머스 다음 큰 숫자, c++ (1) | 2024.02.13 |
| 게임 맵 최단거리 - 프로그래머스, c++ (0) | 2023.02.20 |
| 타겟 넘버 - 프로그래머스, c++ (0) | 2023.02.14 |
| 전력망을 둘로 나누기 - 프로그래머스, c++ (0) | 2023.02.12 |