CS/알고리즘_문제풀이(자바)

조합 0의 개수

Jedy_Kim 2021. 9. 9. 13:25
728x90

문제

n명의 사람중 m명을 순서에 상관없이 뽑는 경우의 수를 조합이라고 하며 nCm으로 나타낸다.

nCm은 수식으로 n!/m!(n-m)! 으로 구할 수 있다. (5! = 1 * 2 * 3 * 4 * 5)

n과 m이 주어졌을때 nCm의 끝자리 0의 개수를 출력하는 프로그램을 작성하시오.  

입력

첫째 줄에 정수 n, m(0≤m≤n≤1,000,000)이 들어온다.

 

출력

첫째 줄에 0의 개수를 출력한다.

 

예제 입력

25 12

예제 출력

2

// 복습이 필요하다.

https://mslim8803.tistory.com/11

// 코드

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
32
33
import java.util.*;
import java.io.*;
 
public class Main{
  
  public int get(int n, int k) {
    int cnt = 0;
    
    while(n >= k) {
      cnt += (n/k);
      n /= k;
    }
    
    return cnt;
  }
  
  public static void main(String[] args) throws Exception {
 
    // Please Enter Your Code Here
    Main sample = new Main();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());
    
    int n = Integer.parseInt(st.nextToken());
    int m = Integer.parseInt(st.nextToken()); 
    
    int twoCnt  = sample.get(n, 2- sample.get(n-m, 2- sample.get(m, 2);
    int fiveCnt = sample.get(n, 5- sample.get(n-m, 5- sample.get(m, 5);
    
    System.out.println(Math.min(twoCnt, fiveCnt));
    
  }
}
cs

 

반응형

'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글

가로수  (0) 2021.09.09
Fly me to the Alpha Centauri  (0) 2021.09.09
벌집  (2) 2021.09.09
소수 찾기  (0) 2021.09.09
분수 합  (0) 2021.09.08