Fork me on GitHub

java-大整数


java中大整数的应用,感觉挺强大的。

原题链接:Java BigInteger

In this problem, you have to add and multiply huge numbers! These numbers are so big that you can’t contain them in any ordinary data types like a long integer.

Use the power of Java’s BigInteger class and solve this problem.

Input Format

There will be two lines containing two numbers, and .

Constraints

and are non-negative integers and can have maximum digits.

Output Format

Output two lines. The first line should contain , and the second line should contain . Don’t print any leading zeros.

Sample Input

1
2
1234
20

Sample Output

1
2
1254
24680

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger a=in.nextBigInteger();
BigInteger b=in.nextBigInteger();
System.out.println(a.add(b));
System.out.println(a.multiply(b));
}
}

原题链接:Java BigDecimal

Java’s BigDecimal class can handle arbitrary-precision signed decimal numbers. Let’s test your knowledge of them!

Given an array, , of real number strings, sort them in descending order — but wait, there’s more! Each number must be printed in the exact same format as it was read from stdin, meaning that is printed as , and is printed as . If two numbers represent numerically equivalent values (e.g., ), then they must be listed in the same order as they were received as input).

Complete the code in the unlocked section of the editor below. You must rearrange array ‘s elements according to the instructions above.

Input Format

The first line consists of a single integer, , denoting the number of integer strings.
Each line of the subsequent lines contains a real number denoting the value of .

Constraints

  • Each has at most 300 digits.

Output Format

Locked stub code in the editor will print the contents of array to stdout. You are only responsible for reordering the array’s elements.

Sample Input

1
2
3
4
5
6
7
8
9
10
9
-100
50
0
56.6
90
0.12
.12
02.34
000.000

Sample Output

1
2
3
4
5
6
7
8
9
90
56.6
50
02.34
0.12
.12
0
000.000
-100

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.math.BigDecimal;
import java.util.*;
class Solution {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String []s = new String[n+2];
for(int i = 0;i < n;i++){
s[i] = sc.next();
}
for(int i = 0;i<n;i++){
BigDecimal max = new BigDecimal(s[i]);
int idx = i;
for(int j = i+1;j<n;j++)
{
BigDecimal curr = new BigDecimal(s[j]);
if(curr.compareTo(max) == 1){
max=curr;
idx=j;
}
}
String temp = s[i];
s[i] = s[idx];
s[idx] = temp;
}
for(int i = 0;i<n;i++){
System.out.println(s[i]);
}
}
}

原题链接:Java Primality Test

A prime number is a natural number greater than whose only positive divisors are and itself. For example, the first six prime numbers are , , , , , and .

Given a large integer, , use the Java BigInteger class’ isProbablePrime method to determine and print whether it’s prime or not prime.

Input Format

A single line containing an integer, (the number to be checked).

Constraints

  • contains at most 100 digits.

Output Format

If is a prime number, print prime; otherwise, print not prime.

Sample Input

1
13

Sample Output

1
prime

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger n = in.nextBigInteger();
in.close();
System.out.println(n.isProbablePrime(10) ? "prime" : "not prime");
}
}

知识点补充:

JAVA BigInteger 成员函数: isProbablePrime

public boolean isProbablePrime(int certainty)

如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。如果 certainty <= 0,则返回 true。

参数:

certainty - 调用方允许的不确定性的度量。如果该调用返回 true,则此 BigInteger 是素数的概率超出 (1 - 1/(2*certainty))。此方法的执行时间与此参数的值是成比例的。

返回:

如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。

Java中的isProbablePrime函数是针对BigInteger类的一个素数判断函数,它的实现原理其实并不复杂,只是要分许多情况讨论,要用到Miller-Rabin素数测试和Lucas-Lehmer测试,它是一个概率算法,返回的结果:一个数不是素数或者一个数可能是素数。

donate the author