두 정수 사이의 합

남태우·2020년 11월 28일
0

문제

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

추상화

a와 b 사이에 속한 모든 정수의 합을 리턴하는 것이기에 반복문을 이용
제한 조건의 a, b가 같을 경우
a, b 둘중 어떤 것이 큰지

코드

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

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

0개의 댓글