개발자일걸요..?

11050번 이항계수1 본문

알고리즘코딩/Baekjoon Online Judge

11050번 이항계수1

Re_A 2021. 2. 14. 21:47
728x90
반응형

문제링크 : 

 

 

<알고리즘>

 1) N을 K번만큼 1씩 감소 시켜가면서 곱셈

 2) K를 K번만큼 1씩 감소 시켜가면서 곱셈

 3) (1)결과값)/(2)결과값) 출력

 

 

방법 1. Scanner를 이용한 입력 ( 메모리 : 12872KB   시간: 128ms ) 

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 count = K;
		
		long up = 1;
		long down = 1;
		while((count--)!=0) {
			up*=(N--);
			down*=(K--);
		}
		
		System.out.println((int)(up/down));
	}
}

 

 

방법2. BufferedReade를 이용한 입력 ( 메모리 : 11516KB   시간 : 76ms )

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
	public static void main(String[] args) {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String s = "";
		try {
			s = bf.readLine();
		}catch(IOException e) {}
		StringTokenizer st = new StringTokenizer(s);
		int N = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		int count = K;
		
		int up =1;
		int down =1;
		while((count--)!=0) {
			up*=(N--);
			down*=(K--);
		}
		System.out.println((int)(up/down));
	}
}

 

 

+) 참고 : ko.wikipedia.org/wiki/%EC%9D%B4%ED%95%AD_%EA%B3%84%EC%88%98

 

이항 계수 - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. 조합론에서, 이항 계수(二項係數, 영어: binomial coefficient)는 이항식을 이항 정리로 전개했을 때 각 항의 계수이며, 주어진 크기의 (순서 없는) 조합의 가짓수이다

ko.wikipedia.org

 

반응형

'알고리즘코딩 > Baekjoon Online Judge' 카테고리의 다른 글

1676번 팩토리얼 0의 개수  (0) 2021.02.15
11051번 이항계수2  (0) 2021.02.14
3036번 링  (0) 2021.02.14
1037번 약수  (0) 2021.02.13
2981번 검문  (0) 2021.02.13
Comments