프로그래머스

프로그래머스 Lv 1 (두 정수 사이의 합)

JeonInwoo 2019. 12. 12. 13:00

풀이 두 정수의 크기 비교 후 크기만큼 for문으로 더해준다

 

코드

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

long long solution(int a, int b) {
    int count = 0;
    long long answer = 0;
    
    if(a > b) {
        count = a-b;
        
        for(int i=0; i<=count; i++) {
            answer += b+i;
        }
    }else if(b > a){
        count = b-a;
        for(int i=0; i<=count; i++) {
            answer += a+i;
        }
    }else {
        return a;
    }
    return answer;
}