专做hiphop的网站企业整站推广
描述
给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除。
输入描述:
两个整数n(2<=n<=1000),a(2<=a<=1000)
输出描述:
一个整数.
示例1
输入:
6 10
输出:
1
代码如下:
import java.math.BigInteger;
import java.util.Scanner;/** 整除问题* * BigInteger:* 1、BigInteger.valueOf()将基本数据类型或字符串转化成BigInteger对象* 2、bI1.compareTo(bI2):bI1与bI2进行比较,如果bI1大于bI2返回1,相等则返回0,小于则返回-1* */
public class IntegralDivisionProblem {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while(scanner.hasNext()) {int n = scanner.nextInt(); int a = scanner.nextInt();BigInteger njc = BigInteger.ONE;//计算n的阶乘for (int i = 2; i <= n; i++) {//BigInteger.valueOf()将基本数据类型或字符串转化成BigInteger对象njc = njc.multiply(BigInteger.valueOf(i));}int time = 0;BigInteger aBI = BigInteger.valueOf(a);//bI1.compareTo(bI2):bI1与bI2进行比较,如果bI1大于bI2返回1,相等则返回0,小于则返回-1while(njc.compareTo(BigInteger.ZERO) == 1 && njc.mod(aBI).compareTo(BigInteger.ZERO) == 0) {time++;//循环计算n的阶乘能被除以多少次,进而求出最大的knjc = njc.divide(aBI);}System.out.println(time);}}}