문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/12912
코딩테스트 연습 - 두 정수 사이의 합
두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요. 예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다. 제한 조건 a와 b가 같은 경우
programmers.co.kr
간단한 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
long long solution(int a, int b) { | |
if (a == b) | |
{ | |
return a; | |
} | |
long long answer = 0; | |
for (int i = min(a, b); i <= max(a, b); i++) | |
{ | |
answer += i; | |
} | |
return answer; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 문자열 압축 (0) | 2021.12.10 |
---|---|
[Programmers] 메뉴 리뉴얼 (0) | 2021.12.09 |
[Programmers] 가운데 글자 가져오기 (0) | 2021.12.07 |
[Programmers] [1차] 비밀지도 (0) | 2021.12.04 |
[Programmers] [1차] 다트 게임 (0) | 2021.11.28 |