文章

20

粉丝

412

获赞

12

访问

163.5k

头像
java实现大数阶乘
推荐阅读
P1368 吉林大学机试题
发布于2021年4月22日 15:49
阅读数 7.4k

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static BigInteger Factorial(int n) {
		BigInteger b = BigInteger.ONE;
		for(int i = 2;i <= n;i++) {
			BigInteger t = new BigInteger(String.valueOf(i));
			b = b.multiply(t);
		}
		return b;
	}
	
	public static BigInteger p(int n, int m) {
		BigInteger ans = Factorial(n).divide(Factorial(n-m));
		return ans;
	}
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n, m, cnt;
		while(input.hasNext()) {
			n = input.nextInt();
			m = input.nextInt();
			if(n == 0 && m == 0) break;
			BigInteger b = p(n, m);
			cnt = 0;
			BigInteger TWO = new BigInteger(String.valueOf(2));
			while(b != BigInteger.ZERO) {
				if(b.remainder(TWO) == BigInteger.ZERO) {
					cnt++;
					b = b.divide(TWO);
				}
				else break;
			}
			System.out.println(cnt);
		}
		input.close();
	}

}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发