Java title set computing large numbers - the proper way questions will not

These topics tql, my mathematical knowledge ah, although most are permutations and combinations, but let me senseless forced large numbers of
so I adopted the java simulation of large numbers
of 10 questions completed seven questions
java operations from the entry of large numbers to do title

Solution to a problem Portal

  • A large number seeking A + B
  • B seeking large number N!
  • Cattleya C + the number of large number of permutations and combinations
  • D large numbers fib
  • E did not do
  • F large numbers fib
  • Large Numbers floating G A + B
  • Large number of permutations and combinations H
  • I did not do, should large numbers simulation questions
  • J did not do

A

Note that formatting on it

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        BigInteger a,b;
        int t;
        t = in.nextInt();
        for(int i=1;i<=t;i++){
            System.out.println("Case "+i+":");

            a = in.nextBigInteger();
            b = in.nextBigInteger();

            System.out.println(a+" + "+b+" = "+a.add(b));
            if(i!=t)System.out.println();
        }
        in.close();

    }
}

B

When large numbers is initialized to type int, using BigInteger.valueOf (i), you can get a BigInteger class
, but I do not understand its internal logic, the use of this function is to convert int Why? Internal BigInteger is how to achieve?

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        BigInteger ans;
        while(in.hasNext()){
            int n = in.nextInt();
            ans = BigInteger.valueOf(1);
            for(int i=1;i<=n;i++){
                ans = ans.multiply(BigInteger.valueOf(i));
            }
            System.out.println(ans);
        }
        in.close();
    
    }
}

C

It is still a bit difficult to understand
the subject, which means that 50 yuan and 100 yuan, the waiter did not change, thanks to $ 50 for $ 100 to find the money, $ 50 means that the customer must before 100 yuan customers first must be 50 yuan customer
portal

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int cnt=1;
        while(in.hasNext()){
            int m = in.nextInt();
            int n = in.nextInt();
            if(n==0&&m==0)break;
            System.out.println("Test #"+cnt+":");
            cnt++;
            System.out.println(getans(m,n));
        }
    }
    public static BigInteger getans(int m,int n){
        BigInteger ans;
        if(m<n)ans = BigInteger.valueOf(0);
        else{
            ans = A(m+n);
            ans = ans.multiply(BigInteger.valueOf(m-n+1));
            ans = ans.divide(BigInteger.valueOf(m+1));
        }
        return ans;
    }
    public static BigInteger A(int n){
        BigInteger ans;
        ans = BigInteger.valueOf(1);
        for(int i=1;i<=n;i++){
            ans = ans.multiply(BigInteger.valueOf(i));
        }
        return ans;
    }
}

D

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        BigInteger a,b,c,d,e,f;
        while(in.hasNext()){
            int n = in.nextInt();
            a = BigInteger.valueOf(1);
            b = BigInteger.valueOf(1);
            c = BigInteger.valueOf(1);
            d = BigInteger.valueOf(1);
            e = BigInteger.valueOf(1);
            for(int i=5;i<=n;i++){
                e = a.add(b);
                e = e.add(c);
                e = e.add(d);
                a = b;
                b = c;
                c = d;
                d = e;
            }
            System.out.println(e);
        }
    }
}

F

How do you assign initialization time?

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        BigInteger a,b,c;
        for(int i=1;i<=t;i++){
            int n = in.nextInt();
            a = BigInteger.valueOf(0);
            b = BigInteger.valueOf(1);
            c = a.add(b);
            for(int j=2;j<=n;j++){
                c = b;
                b = a.add(b);
                a = c;
            }
            System.out.println(b);
        }
        in.close();
    }
}

G

Large real number operation
, but the need to remove the decimal point to 0, and without scientific notation

stripTrailingZeros()
toPlainString()

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        BigDecimal a,b;
        while(in.hasNext()){
            a = in.nextBigDecimal();
            b = in.nextBigDecimal();
            System.out.println(a.add(b).stripTrailingZeros().toPlainString());//所以用stripTrailingZeros()函数去除末尾0,用toPlainString()函数转成普遍计数法输出。
        }
        in.close();

    }
}

H

0 2, 1 2, 2 2, 3 2 ... n / 2 Items 2
1 2 (. 1-n-)
2 Items 2 ((. 4-n-) +2)
.
.
.

And n-<= 200 is
ANS =. 1;
for (int I =. 1; I <= n-/ 2; I ++) {// Representative number 2
ans + = C ((n- 2 * i) + i, i);
is = C + ANS (Ni, I);
}

in.next () and in.nextLine ()
the Next () must be read to be valid after the end of the input character, symbol of the end of the spacebar encountered before entering a valid character, Tab or Enter keys, etc., next () the method will automatically be removed only after you enter a valid character, next () method was subsequently entered the spacebar, Tab key or the enter key, etc. as a separator or terminator
and nextLine () method of just enter terminator key, i.e. nextLine () method returns all of the characters before the Enter key, it is possible to obtain a string with spaces.

import java.util.*;
import java.math.*;
import java.lang.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int t;
        t = in.nextInt();
        for(int i=1;i<=t;i++){
            String x = in.next();
            int n = x.length();
            BigInteger ans;
            ans = BigInteger.valueOf(1);
            for(int j = 1;j<=n/2;j++){
                ans = ans.add(C(n-i,i));
            }
            System.out.println(ans);
        }
    }
    public static BigInteger C(int n,int m){
        if(n<m)return BigInteger.valueOf(0);
        if(n==m)return BigInteger.valueOf(1);
        BigInteger ans;
        ans = BigInteger.valueOf(1);
        int mx = Math.max(n-m,m);
        int mi = n-mx;
        for(int i=1;i<=mi;i++){
            ans = ans.multiply(BigInteger.valueOf(mx+i));
            ans = ans.divide(BigInteger.valueOf(i));
        }
        return ans;
    }
}

Guess you like

Origin www.cnblogs.com/Emcikem/p/11705889.html