프로그래머스(Programmers)
두 정수 사이의 합
cosmohoo
2022. 7. 5. 23:43
반응형

=> a 와 b 중 큰수를 찾습니다.
=> 작은 수에서부터 큰수까지 for문을 돌며 answer에 더합니다.
*시그마 공식을 이용해서 풀이할 수도 있습니다.

#include <string>
#include <vector>
using namespace std;
long long solution(int a, int b) {
long long answer = 0;
if(a < b)
{
for(int i=a; i<=b; i++) answer+=i;
}
else
{
for(int i=b; i<=a; i++) answer+=i;
}
return answer;
}반응형