Cartland number (Java) + java basic knowledge

Keywords: Java Eclipse

Enter the eclipse interface

Step 1: File - > New - > java project - > name - > finish

Step 2: enter the project you just built, right-click SRC - > New - > package - > name - > finish

Step 3: enter the package you just created, right-click name - > New - > class - > name (note that if you hand in java code during the competition, the class name here should be named Main, case sensitive)

Here's the code of catland number

import java.math.*;
import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        int n;
        Scanner cin = new Scanner(System.in);   
        BigInteger[][]a=new BigInteger [110][110];   
        for(int i=0;i<101;i++)
            for(int j=0;j<101;j++)
                a[i][j]=BigInteger.valueOf(0);
        for(int i=1;i<101;i++)
            a[i][0]=BigInteger.valueOf(1);
        for(int i=1;i<101;i++)
            for(int j=1;j<=i;j++)
            {
                a[i][j]=a[i][j].add(a[i-1][j]);
                a[i][j]=a[i][j].add(a[i][j-1]);
            }
        while(cin.hasNext())   //Multi group input
        {
            n=cin.nextInt();
            BigInteger ans=BigInteger.ONE;  //Assign an initial value of 1 to ans
            //Equals BigInteger ans=new BigInteger("1");
            ans=a[n][n];
            System.out.println(ans);   //Line feed output
        }
        cin.close();

    }
}

Here are some big numbers in java
1. Addition, subtraction, multiplication and division of large integers

    package BigInteger;
    import java.util.*;
    import java.math.*;
    public class Main {
        public static void main(String args[]) {

            Scanner cin = new Scanner(System.in);

            BigInteger a , b;
            a=cin.nextBigInteger();
            b=cin.nextBigInteger();

            BigInteger ans_add,ans_sub,ans_mul,ans_div,ans_mod;

            ans_add = a.add(b); //a+b
            ans_sub = a.subtract(b); //a-b
            ans_mul = a.multiply(b); //a*b
            ans_div = a.divide(b); //a/b
            ans_mod = a.mod(b); //a%b
            System.out.println("a + b = "+ans_add);
            System.out.println("a - b = "+ans_sub);
            System.out.println("a * b = "+ans_mul);
            System.out.println("a / b = "+ans_div);
            System.out.println("a % b = "+ans_mod);

        }
    }

2. Realize two large numbers to be complementary or divided

import java.util.*;
import java.math.*;


public class Main {
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        String c;
        BigInteger a,b;
        while(cin.hasNext())
        {
            a=cin.nextBigInteger();
            c=cin.next();
            b=cin.nextBigInteger();
            BigInteger ans=new BigInteger("1");
            if(c.equals(String.valueOf("%")))
                ans=a.mod(b);
            if(c.equals(String.valueOf("/")))
            // The function of valueof transforms s into c-type
            //String s="12345";
            //BigInteger c=BigInteger.valueOf(s); then c=12345;
                ans=a.divide(b);

            System.out.println(ans);
        }


    }
}

3. Remove the redundant 0 and 0 before the decimal point

import java.util.*;
import java.math.*;


public class Main {
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        BigDecimal a;
        int b;
        while(cin.hasNext())
        {
            a=cin.nextBigDecimal();  //Large decimal fraction
            b=cin.nextInt();
            BigDecimal ans=new BigDecimal("1");
            for(int i=1;i<=b;i++)
                ans=ans.multiply(a);
            String str;   //Define a character
            str=ans.stripTrailingZeros().toPlainString();   //Remove last 0
            if(str.charAt(0)=='0')  //Remove 0 before decimal point
                System.out.println(str.substring(1));
            else
                System.out.println(str);



        }


    }
}

4. Compare the size between two numbers

import java.util.*;
import java.math.*;


public class Main {
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        BigInteger m = new BigInteger("2147483647");  //Maximum of int type
        BigInteger a,b,ans;
        String p,op,q;
        while(cin.hasNext())
        {
            p=cin.next();
            op=cin.next();
            q=cin.next();
            System.out.println(p+" "+op+" "+q);  //Output between two with + sign
            a=new BigInteger(p,10); //Converts the specified string to a decimal representation
            b=new BigInteger(q,10);
            ans=new BigInteger("0");
            if(op.charAt(0)=='+')
                ans=a.add(b);
            if(op.charAt(0)=='*')
                ans=a.multiply(b);


            if(a.compareTo(m)>0)    //Comparing two strings
                System.out.println("first number too big");
            if(b.compareTo(m)>0)
                System.out.println("second number too big");
            if(ans.compareTo(m)>0)
                System.out.println("result too big");


        }
        cin.close();


    }
}

Posted by saqibb on Fri, 31 Jan 2020 12:10:21 -0800