Java Big Data trainer

Source: Internet
Author: User

Today, I suddenly saw the big number questions on OJ, because I learned a little big number knowledge. Decisive 6 questions ...... Is very basic. It's just a matter of practice.

What I learned is only the basic operations of some big data classes. I will do more such questions in the future, and strive to be proficient in using big data questions...

Big Data factorial

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 28

The Code is as follows:

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

Board coverage

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 45

The Code is as follows:

import java.math.BigInteger;import java.util.*;import java.io.*;public class Main{public static void main(String args[]){Scanner in = new Scanner(System.in);int test = in.nextInt();while(test-- > 0){int n;n = in.nextInt();BigInteger a = new BigInteger("4");for(int i = 1; i < n; ++i)a = a.multiply(BigInteger.valueOf(4));System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));}}}

Compare size

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 73

The Code is as follows:

import java.io.*;import java.math.BigInteger;import java.util.*;public class Main{public static void main(String args[]){Scanner cin = new Scanner(System.in);while(cin.hasNext()){BigInteger a = cin.nextBigInteger();BigInteger b = cin.nextBigInteger();if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO))break;int flag = a.compareTo(b);if(flag == -1)System.out.println("a<b");else if(flag == 0)System.out.println("a==b");elseSystem.out.println("a>b");}}}

Addition of large numbers

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 103

The Code is as follows:

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

Recursive Evaluation

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 114

The Code is as follows:

import java.io.*;import java.math.BigInteger;import java.util.*;public class Main{public static void main(String args[]){Scanner cin = new Scanner(System.in);BigInteger a[] = new BigInteger[100];while(cin.hasNext()){for(int i = 0; i <= 2; ++i)a[i] = cin.nextBigInteger();for(int i = 3; i <= 99; ++i)a[i] = a[i - 1].add(a[i - 2]).add(a[i - 3]);System.out.println(a[99]);}}}

High Precision power

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 155

The Code is as follows:

Import Java. io. *; import Java. math. bigdecimal; import Java. util. *; public class main {public static void main (string ARGs []) {program CIN = new program (system. in); While (CIN. hasnext () {bigdecimal ans = cin. nextbigdecimal (); int n = cin. nextint (); string res = ans. pow (n ). striptrailingzeros (). toplainstring (); // The integer removes the decimal point and the 0if (res. startswith ("0") // remove the leading 0 {res = res. substring (1);} system. out. println (RES );}}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.