일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 시큐리티
- Spring boot
- 프로그래머스
- programmers
- 2차원배열
- 소스트리
- html
- JAVA11
- gradle
- css
- 싱글톤
- 로그인
- codingtest
- 코딩테스트
- 시큐리티로그인
- springSecurity
- input태그
- 목록
- 리눅스
- 시큐리티 로그인
- 스프링 부트
- springboot
- StyleSheet
- 반복문
- javascript
- java
- sql
- security
- Linux
- 시큐리티 로그아웃
Archives
- Today
- Total
JAVAIARY
프로그래머스 ) 구슬을 나누는 경우의 수 본문
문제: https://school.programmers.co.kr/learn/courses/30/lessons/120840
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.math.BigInteger;
class Solution {
public BigInteger solution(int intBalls, int intShare) {
// balls! / ((balls-share)! * share!)
String strBalls = Integer.toString(intBalls);
String strShare = Integer.toString(intShare);
BigInteger balls = new BigInteger(strBalls);
BigInteger share = new BigInteger(strShare);
BigInteger st= balls.subtract(share);
return factorial(balls).divide((factorial(st).multiply(factorial(share))) );
}
public static BigInteger factorial(BigInteger a) {
if (a.compareTo(BigInteger.ZERO) == 1 ) {
return factorial(a.subtract(BigInteger.ONE)).multiply(a);
}
return BigInteger.ONE;
}
}
- 하노이의 탑을 통해 매운맛을 보고 재귀함수를 연습하기 위해 풀었던 문제
- 재귀함수 자체는 어려운 게 없었지만 BigInteger라는 자료형을 알아야만 풀 수 있었던 문제였다
BigInteger
- 범위에 제한이 없는 숫자 자료형
- 기본적인 연산자( + - / * )로 연산을 할 수 없기 때문에 자체 함수를 사용해야함
- multiply() , subtract() ....
'examplePractice' 카테고리의 다른 글
프로그래머스) 자연수 뒤집어 배열로 만들기 (0) | 2023.04.18 |
---|---|
프로그래머스 ) 이상한 문자 만들기 (0) | 2023.04.10 |
프로그래머스 ) 최소 직사각형 (0) | 2023.04.04 |
프로그래머스 ) 이진 변환 반복하기 (0) | 2023.04.04 |
프로그래머스 ) n^2 배열 자르기 (0) | 2023.04.03 |