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
|
|
Sample Output
|
|
代码如下:
|
|
原题链接: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
|
|
Sample Output
|
|
代码如下:
|
|
原题链接: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
|
|
Sample Output
|
|
代码如下:
|
|
知识点补充:
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测试,它是一个概率算法,返回的结果:一个数不是素数或者一个数可能是素数。