두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요.
예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다.
- a와 b가 같은 경우는 둘 중 아무 수나 리턴하세요.
- a와 b는 -10,000,000 이상 10,000,000 이하인 정수입니다.
- a와 b의 대소관계는 정해져있지 않습니다.
a b return 3 5 12 3 3 3 5 3 12
class Solution {
public long solution(int a, int b) {
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;
}
}
class Solution {
public long solution(int a, int b) {
long answer = 0;
int start = Math.min(a, b);
int end = Math.max(a, b);
for (int i = start; i <= end; i++) {
answer += i;
}
return answer;
}
}
처음에 저렇게 생각했다가 아니, b가 a보다 크면 우째?! 이렇게 했다가 막혔다 정말 1을 생각하면 그 이상 생각을 못하는게 조낸 바보같음ㅗㅗ 암튼 정말 간단한 문제..
Math. 를 써서 풀 수도 있음!!