显示输出的特定数字 // 从 bigInteger 中删除 0



如何仅显示输出的特定数字?我如何从biginteger删除0?

我的示例:

有一个任务以显示数字的阶乘的最后一个数字,而不是0。

Example:
1! = 1
2! = 2
3! = 6
4! = 4
5! = 2
6! = 2

现在它只是显示阶乘。

import java.math.BigInteger;
import java.util.Scanner;
public class Main{
    // 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
    {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        System.out.println(factorial(n));
    }
}

示例vol2->在这里我需要一个解决方案才能从biginteger删除0:

import java.math.BigInteger;
import java.util.Scanner;
public class Main{
    // 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
    {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        BigInteger a = (factorial(n));
        BigInteger b, c;
        c = new BigInteger("10");
        b = a.mod(c);
        System.out.println(b);
        System.out.println(a);
    }
}

是否有一种简单的方法可以从数字中删除所有0?那将是解决我的问题的最简单方法

您可以将您的号码转换为字符串并删除零。然后,您将其放回BigInteger

public static BigInteger removeZeroes(int i) {
    return new BigInteger(String.valueOf(i).replace("0", ""));
}

最新更新