공부/Algorithms w.Java

백준 2407; java

thegreatjy 2021. 12. 11. 00:02
728x90
import java.math.BigInteger;
import java.util.*;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int m=scan.nextInt();
		
		BigInteger up=new BigInteger("1");
		BigInteger down=new BigInteger("1");
		for(int i=0; i<m; i++) {
			up=up.multiply(BigInteger.valueOf(n-i));
			down=down.multiply(BigInteger.valueOf(i+1));
		}
		up=up.divide(down);
		System.out.print(up);
	}

}

 

처음에는 분자, 분모를 위한 for문을 두 번 돌린 후, 나눠 결과값을 구해주었다. 근데 자꾸 틀렸다고 해서,,

구글링해서,, for문 한 번에 분자, 분모 값을 구하는 방법을 알게 되었다.. 

또 한 번 자괴감,, 후,, 쉽지 않네... !!

그리고 BigInteger 클래스 처음 알았다. 


- 참고 블로그

https://coding-factory.tistory.com/604

 

- BigInteger

int : 4bytes, long : 8bytes

무한대의 정수를 사용하고 싶을 때에는 BigInteger 클래스 사용

문자열 형태이다.

 

import java.math.BigInteger;

BigInteger bi = new Biginteger("100");

 

- BigDecimal

 

 

728x90