-
프로그래머스 Lv 1 (두 정수 사이의 합)프로그래머스 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; }
'프로그래머스' 카테고리의 다른 글
프로그래머스 Lv2 (최대값, 최소값 구하기) (0) 2020.02.04 프로그래머스 Lv 1 (2016년) (0) 2020.01.06 프로그래머스 Lv2 (전화번호 목록) (0) 2019.12.16 프로그래머스 Lv1 (가운데 글자 가져오기) (0) 2019.12.12 프로그래머스 Lv1 (홀, 짝 구하기) (0) 2019.12.07