BigInteger 常用函数


        BigInteger big=new BigInteger("2");
        BigInteger temp=new BigInteger("6");

        System.out.println(big.multiply(temp));                   *
        System.out.println(big.pow(2));                          pow
        System.out.println(big.remainder(temp));                  %
    
        System.out.println(big.divide(temp));              
         System.out.println(big.gcd(temp));      

        System.out.println(big.xor(temp));    big 和temp 符号不同时,相加,返回正    相同时,相互减,返回整

--------------------------------------------------------------------------------------------------------------------


    输入n,计算s = 1!+2!+3!+……+n!的末六位(不含前导0)。你<=10^6,n!表示前两个样例之和。
    样例输入:

    10

    样例输出:

    37913
    //考虑数的长度溢出。


--------------------------------------------------------------------------------------------------------------------------------------


import java.math.BigInteger;

class Length_p{
    private int num;
    private BigInteger big=new BigInteger("0");
    private BigInteger temp=new BigInteger("1");  
    public Length_p(int num) {
        super();
        this.num = num;
        fun();

    }
    
    
    public void fun(){
        for(int i=1;i<=this.num;i++)        //   将1+num 的阶乘放在big 调用TempFun()函数
        {
            TempFun(i);

            big=big.add(temp);

            temp=temp.pow(0);        //temp 数据清空
        }
    }
    


    public void TempFun(int i){   //求出函数的阶乘,并放在temp   3!=1*2*3
        for(int t=1;t<=i;t++){        
            temp=temp.multiply(new BigInteger(String.valueOf(t)));
        }
        
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        String str=big.toString();
    //    System.out.println(str);
        int i=str.lastIndexOf("0");
        str=str.substring(i+1, str.length());
        
        return str;
    }
}
public class Length_Extent {
    public static void main(String[] args) {
        Length_p l=new Length_p(10);
        System.out.println(l);
    

    }
    
    

}


你可能感兴趣的:(java_ee)