두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요.
예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다.
a | b | return |
---|---|---|
3 | 5 | 12 |
3 | 3 | 3 |
5 | 3 | 12 |
#include <string>
#include <vector>
using namespace std;
long long solution(int a, int b) {
long long answer = 0;
int small = 0;
int big = 0;
if (a > b)
{
big = a;
small = b;
}
else if (a < b)
{
big = b;
small = a;
}
else {
return a;
}
for (int i = small; i <= big; ++i)
{
answer += i;
}
return answer;
}