본문 바로가기
Algorithm

3 . 프로그래머스_두 정수 사이의 합

by 오주현 2021. 9. 20.
반응형

프로그래머스_두 정수 사이의 합

 

프로그래머스 두 정수 사이의 합

 

https://programmers.co.kr/learn/courses/30/lessons/12912

 

-

의사 코드

조건문으로 a, b값 크기 차이에 따른 경우의 수를 모두 잡고 둘 다 for문을 사용해 전역변수에 값을 누적시켰습니다.

 

-

 

정답 코드

class Solution {
    public long solution(int a, int b) {
        long answer = 0;
        long hap = 0;
        
        if(a<=b){
            for(int i = a; i <= b; i++){
                hap+=i;
            }
        } else {
            for(int i = b; i <= a; i++){
                hap+=i;
            }
        }
        answer = hap;
        return answer;
    }
}

 

 

 

 

 

 

 

반응형

댓글