빡코

[과제]프로그래머스_두 정수 사이의 합 본문

Algorithm

[과제]프로그래머스_두 정수 사이의 합

chris.djang 2020. 1. 2. 23:41

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가 같은 경우는 둘 중 아무 수나 리턴하세요. a와 b는 -10,000,000 이상 10,000,000 이하인 정수입니다. a와 b의 대소관계는 정해져있지 않습니다. 입출력 예 a b return 3 5 12 3 3 3 5 3

programmers.co.kr

 

package programers;

import java.util.Arrays;

public class Solution1 {

	public long solution(int a, int b) {
		long answer = 0;
		int[] num = {a,b};
		Arrays.sort(num);
		int a1 = num[0];
		int b1= num[1];
		if(a1!=b1) {
			for(int i=a1;i<=b1;i++) {
				answer+=i;
			}
		}else{
			answer = a1;
		}
		return answer;
	}

	public static void main(String[] args) {

		Solution1 sl = new Solution1();

		System.out.println(sl.solution(5, 3));

	}

}

'Algorithm' 카테고리의 다른 글

백준_1931번_회의실배정  (0) 2020.01.16
백준_11399번_ATM  (0) 2020.01.15
[과제] 설명 준비해오기1  (0) 2020.01.02
[ 과제]백준_ 11047번_동전0  (0) 2020.01.02
[과제]백준_ 10872번 _팩토리얼  (0) 2020.01.02