개발

백준 19939 본문

Java/Algorithm

백준 19939

Dev.hs 2020. 11. 18. 22:09

자바 풀이

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//공의갯수
		int N = sc.nextInt();
		//팀의수
		int K = sc.nextInt();
		
		int[] arr = new int[K];
		
		int ballCnt = 0;
		
		for(int i = 0; i < K; i++) {
			arr[i] = i+1;
			ballCnt += i+1;
		}
		
		//최소로주고 남은 공의 수
		int addBall = N-ballCnt;
		
		//공이 부족할경우
		if(N < ballCnt) {
			System.out.println(-1);
			return;
		//최소로만 줬을경우
		} else if(addBall == 0) {
			System.out.println(K-1);
			return;
		}
		
 		//공이 남을경우
		int J = K-1;
		
		for(int i = addBall; i > 0; i--) {
			arr[J] = arr[J]+1;
			J--;
			if(J == -1) {
				J = K-1;
			}
		}
		System.out.println(arr[K-1]-arr[0]);
	}
}

'Java > Algorithm' 카테고리의 다른 글

백준 1459  (0) 2021.02.08
백준 2589  (0) 2021.01.25
프로그래머스 위장  (0) 2021.01.23
백준 2596  (0) 2020.11.25
백준 자바 런타임에러(입력값)  (0) 2020.11.22
Comments