👇소스 코드
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;
}
}
🐣코드 설명
1. Math함수를 이용하여 start와 end값을 구한다. ( 받아오는 a와 b값을 비교해서 작은거는 start, 큰거는 end에 담아둔다. )
2. for문을 사용해서 answer에 start부터 end까지 숫자를 더해준다.
728x90
'Algorithm > Programmers' 카테고리의 다른 글
프로그래머스 ) flag에 따라 다른 값 반환하기 [JAVA/자바] - 오늘의 TIL (0) | 2024.02.11 |
---|---|
프로그래머스 ) 서울에서 김서방 찾기 [JAVA/자바] - 오늘의 TIL (0) | 2024.02.07 |
프로그래머스 ) [PCCE 기출문제] 1번 / 출력 [C++] - 오늘의 TIL (0) | 2024.02.04 |
프로그래머스 ) 역순 정렬하기 [SQL] - 오늘의 TIL (0) | 2023.10.02 |
프로그래머스 ) 모든 레코드 조회하기 [SQL] - 오늘의 TIL (0) | 2023.10.01 |