import java.math.BigInteger;
import java.util.Scanner;
public class LastFact
{
// Returns Factorial of N
static BigInteger factorial(int N)
{
// Initialize result
BigInteger f = new BigInteger("1"); // Or BigInteger.ONE
// Multiply f with 2, 3, ...N
for (int i = 2; i <= N; i++)
f = f.multiply(BigInteger.valueOf(i));
return f;
}
// Driver method
public static void main(String args[]) throws Exception
{
int N = 300;
System.out.println(factorial(N));
}
}
发布于 2018-08-01 20:26:36
如果您想要删除BigInteger
的最后一位N
数字,请将其除以10^N
,例如,删除最后两位数字除以10^2=100
BigInteger i = new BigInteger("123456");
BigInteger j = i.divide(BigInteger.valueOf(100)); // j=1234
要获得要删除的提醒,请改用reminder()
方法:
BigInteger i = new BigInteger("123456");
BigInteger j = i.reminder(BigInteger.valueOf(100)); // j=56
请注意,由于您正在计算较大的阶乘,因此您可能希望使用Stirling's approximation。
发布于 2018-08-01 20:26:44
取最后一位数,得到BigInteger
除以10后的余数
System.out.println(f.remainder(BigInteger.TEN));
发布于 2018-08-01 20:28:43
将你的大整数转换成字符串,然后取最后一个数字。
public static void main(String args[]) throws Exception
{
int N = 300;
String bigNumber = factorial(N).toString();
String lastNumberStr = bigNumber.charAt(bigNumber.length()-1)+"";
int lastNumber = Integer.parseInt(lastNumberStr);
System.out.println(lastNumber);
}
https://stackoverflow.com/questions/51633188
复制相似问题