前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java-大整数

java-大整数

作者头像
luxuantao
发布2021-02-24 11:24:07
5240
发布2021-02-24 11:24:07
举报
文章被收录于专栏:Fdu弟中弟Fdu弟中弟
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

代码语言:javascript
复制
1234
20

Sample Output

代码语言:javascript
复制
1254
24680

代码如下:

代码语言:javascript
复制
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

代码语言:javascript
复制
9
-100
50
0
56.6
90
0.12
.12
02.34
000.000

Sample Output

代码语言:javascript
复制
90
56.6
50
02.34
0.12
.12
0
000.000
-100

代码如下:

代码语言:javascript
复制
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

代码语言:javascript
复制
13

Sample Output

代码语言:javascript
复制
prime

代码如下:

代码语言:javascript
复制
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测试,它是一个概率算法,返回的结果:一个数不是素数或者一个数可能是素数。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-09-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档