728x90
반응형
SMALL
문제 설명
두 정수 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 |
package LV1;
public class H12912 {
public static long solution(int a, int b) {
long answer = 0;
if (a != b){
for (int i = Math.min(a, b); i <= Math.max(a, b); i++) {
answer += i;
}
}else {
answer = a;
}
return answer;
}
public static void main(String[] args){
int a1 = 3;
int b1 = 5;
int a2 = 3;
int b2 = 3;
int a3 = 5;
int b3 = 3;
System.out.println(solution(a1, b1));
System.out.println(solution(a2, b2));
System.out.println(solution(a3, b3));
}
}
728x90
반응형
LIST
'알고리즘 > 프로그래머스 JAVA LV.1' 카테고리의 다른 글
없는 숫자 더하기 (0) | 2023.02.01 |
---|---|
문자열을 정수로 바꾸기 (0) | 2023.02.01 |
가운데 글자 가져오기 (0) | 2023.02.01 |
짝수와 홀수 (0) | 2023.02.01 |
직사각형 별찍기 (0) | 2023.02.01 |