java大整数类

1、大整数的赋值 、输入、输出(三种)

package 做题;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
 
public class Main 
{
    public static void main(String[] args)
    {
    	Scanner sc = new Scanner(System.in);
    	while(sc.hasNext())  // 多组输入
    	{
             // 赋值
		     BigInteger a = new BigInteger("12345");
		     
             // 读入
		     BigInteger b;	
		     b = sc.nextBigInteger();
		     
             // 输出
		     System.out.println(a);
		     System.out.printf("%d\n",b);
             System.out.println(a + " " + b);
    	 }
	}
}

2、大整数的加减乘除

package 做题;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

public class Main 
{
    public static void main(String[] args)
    {
    	Scanner sc = new Scanner(System.in);
    	while(sc.hasNext())  // 多组输入
    	{
		     BigInteger a;
		     BigInteger b;
		
		     a = sc.nextBigInteger();
		     b = sc.nextBigInteger();
		
		     BigInteger res1 = a.add(b);       // a + b
		     BigInteger res2 = a.subtract(b);  // a - b
		     BigInteger res3 = a.multiply(b);  // a * b
		     BigInteger res4 = a.divide(b);    // a / b
		     BigInteger res5 = a.remainder(b); // a % b
		
		     System.out.println(res1);
		     System.out.println(res2);
		     System.out.println(res3);
		     System.out.println(res4);
		     System.out.println(res5);
    	 }
	}
}

3、常用方法

方法名作用
add();相加
subtract();相减
multipy();相乘
divide();相除
remainder();取余
pow();n次方
abs();绝对值
negate();相反数
gcd();最大公约数
mod();相当于remainder
min();两者中的最小值
max();两者中的最大值
and();按位与
andNot();按位与非
not();当前大数的非
or();按位或
xor();按位异或
shiftLeft();当前大数左移n位
shiftRight();当前大数右移n位
bitLength();当前大数的二进制位数
compareTo();大数比较大小

3、int转BigInteger

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.function.BiFunction;


public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)1e5 + 10;

    public static void main(String[] args) throws NumberFormatException, IOException
    {
        int n = rd.nextInt();

        //方法2:主打万能简洁
        BigInteger res1 = BigInteger.valueOf(n);

        // 方法2:主打原始
        String s = "" + n;
        BigInteger res2 = new BigInteger(String.valueOf(s));

        pw.println(res1);
        pw.println(res2);
        pw.flush();
    }
}

class MyComparator implements Comparator<Integer>
{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII implements Comparable<PII>
{
    long x,y;
    public PII(long x ,long y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.y-a.y != 0)
            return Math.toIntExact(this.y - a.y);  //按x升序排序
        else return Math.toIntExact(this.x - a.x);  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

4、具体应用

试题 A: 星期计算【填空题】本题总分: 5分  得5分

【问题描述】

    已知今天是星期六,请问 20^22 天后是星期几?
    注意用数字 1 到 7 表示星期一到星期日

【答案提交】

    这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

AC代码:

import java.io.*;
import java.math.BigInteger;
import java.util.*;
 
public class Main
{
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = 5010;
    static int a[] = new int[N];
 
    public static void main(String[] args) throws IOException
    {
        // n为经过了多少天
        BigInteger n = BigInteger.valueOf(20).pow(22).mod(BigInteger.valueOf(7));
 
        // 假设现在是周1,那么经过6天,变成周7  ,输入结果就是(1 + 6) % 7,虽然是0,但是是周天;如果经过2天,就是(1 + 2) % 7 = 3,答案就是周3;同理,周六经过2天,就是(6 + 2)% 7 = 1,答案就是周一
 
        // 假设
        int res = (6 + n.intValue()) % 7;
 
        res = res == 0 ? 7: res;
        out.println(res);
        out.flush();
    }
}
 
class in
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");
 
    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(in.nextLine());
        return d;
    }
}
 
class PII
{
    int x,y;
    public PII(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

AC代码:

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.function.BiFunction;


public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)1e5 + 10;

    public static void main(String[] args) throws NumberFormatException, IOException
    {
        long n = rd.nextLong();

        BigInteger sum = BigInteger.ZERO;
        for(long i = 1  ; i <= n ; i ++)
        {
            BigInteger temp = BigInteger.valueOf(i);
            sum = sum.add(temp.multiply(temp).multiply(temp).multiply(temp).multiply(temp).multiply(temp).multiply(temp).multiply(temp));
        }
        pw.println(sum.mod(BigInteger.valueOf(123456789)));
        pw.flush();
    }
}

class MyComparator implements Comparator<Integer>
{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII implements Comparable<PII>
{
    long x,y;
    public PII(long x ,long y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.y-a.y != 0)
            return Math.toIntExact(this.y - a.y);  //按x升序排序
        else return Math.toIntExact(this.x - a.x);  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

 

 

 

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

21RGHLY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值