Java topic practiced hand

Source: Internet
Author: User

Large number factorial

Title Link: http://acm.nyist.net/JudgeOnline/problem.php?pid=28

The code is as follows:

[Java]View Plaincopy
  1. Import java.io.*;
  2. Import Java.math.BigInteger;
  3. Import java.util.*;
  4. Public class Main
  5. {
  6. public static void Main (String args[])
  7. {
  8. Scanner cin = new Scanner (system.in);
  9. int n = cin.nextint ();
  10. BigInteger ans = biginteger.one;
  11. For (int i = 1; I <= n; ++i)
  12. Ans = ans.multiply (biginteger.valueof (i));
  13. System.out.println (ANS);
  14. }
  15. }

Board Overlay

Title Link: http://acm.nyist.net/JudgeOnline/problem.php?pid=45

The code is as follows:

[Java]View Plaincopy
  1. Import Java.math.BigInteger;
  2. Import java.util.*;
  3. Import java.io.*;
  4. Public class Main
  5. {
  6. public static void Main (String args[])
  7. {
  8. Scanner in = new Scanner (system.in);
  9. int test = In.nextint ();
  10. While (test--> 0)
  11. {
  12. int n;
  13. n = in.nextint ();
  14. BigInteger a = new BigInteger ("4");
  15. For (int i = 1; i < n; ++i)
  16. A = A.multiply (biginteger.valueof (4));
  17. System.out.println (A.subtract (biginteger.valueof (1)). Divide (Biginteger.valueof (3));
  18. }
  19. }
  20. }

Compare size

Title Link: http://acm.nyist.net/JudgeOnline/problem.php?pid=73

The code is as follows:

[Java]View Plaincopy
  1. Import java.io.*;
  2. Import Java.math.BigInteger;
  3. Import java.util.*;
  4. Public class Main
  5. {
  6. public static void Main (String args[])
  7. {
  8. Scanner cin = new Scanner (system.in);
  9. While (Cin.hasnext ())
  10. {
  11. BigInteger a = Cin.nextbiginteger ();
  12. BigInteger B = Cin.nextbiginteger ();
  13. if (a.equals (Biginteger.zero) && b.equals (Biginteger.zero))
  14. Break ;
  15. int flag = A.compareto (b);
  16. if (flag = =-1)
  17. System.out.println ("a<b");
  18. Else if (flag = = 0)
  19. System.out.println ("a==b");
  20. Else
  21. System.out.println ("a>b");
  22. }
  23. }
  24. }

Large number of additions

Title Link: http://acm.nyist.net/JudgeOnline/problem.php?pid=103

The code is as follows:

[Java]View Plaincopy
  1. Import Java.math.BigInteger;
  2. Import java.util.*;
  3. Import java.io.*;
  4. Public class Main
  5. {
  6. public static void Main (String args[])
  7. {
  8. Scanner in = new Scanner (system.in);
  9. int n = in.nextint ();
  10. For (int i = 1; I <= n; ++i)
  11. {
  12. BigInteger a = In.nextbiginteger ();
  13. BigInteger B = In.nextbiginteger ();
  14. BigInteger ans = a.add (b);
  15. System.out.println ("case" + i + ":");
  16. System.out.println (A + "+" + B + "=" +ans);
  17. }
  18. }
  19. }

Recursive value

Title Link: http://acm.nyist.net/JudgeOnline/problem.php?pid=114

The code is as follows:

[Java]View Plaincopy
  1. Import java.io.*;
  2. Import Java.math.BigInteger;
  3. Import java.util.*;
  4. Public class Main
  5. {
  6. public static void Main (String args[])
  7. {
  8. Scanner cin = new Scanner (system.in);
  9. BigInteger a[] = new biginteger[100];
  10. While (Cin.hasnext ())
  11. {
  12. For (int i = 0; I <= 2; ++i)
  13. A[i] = Cin.nextbiginteger ();
  14. For (int i = 3; I <= ; ++i)
  15. A[i] = a[i- 1].add (A[i- 2]). Add (A[i- 3]);
  16. System.out.println (a[99]);
  17. }
  18. }
  19. }

High precision Power

Title Link: http://acm.nyist.net/JudgeOnline/problem.php?pid=155

The code is as follows:

[Java]View Plaincopy
  1. Import java.io.*;
  2. Import Java.math.BigDecimal;
  3. Import java.util.*;
  4. Public class Main
  5. {
  6. public static void Main (String args[])
  7. {
  8. Scanner cin = new Scanner (system.in);
  9. While (Cin.hasnext ())
  10. {
  11. BigDecimal ans = cin.nextbigdecimal ();
  12. int n = cin.nextint ();
  13. String res = ANS.POW (n). Striptrailingzeros (). toplainstring (); //integer minus the decimal point and the following 0
  14. if (res.startswith ("0")) //Remove leading 0
  15. {
  16. res = res.substring (1);
  17. }
  18. System.out.println (RES);
  19. }
  20. }
  21. }

Java topic practiced hand

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.