[Hanani]JAVA大数相关学习记录

时间:2024-05-01 20:56:40

1.Basic remains 题目链接
涉及内容:
|大数读入|大数模|大数进制读入时转化为十进制|大数输出时转化为其他进制输出|

import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;//各种头文件
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);//读入
        int b;
        BigInteger p,m;
        while(cin.hasNextInt()) {
            b = cin.nextInt();
            if(b==0) break;
            p = cin.nextBigInteger(b);//把读入的各种进制转化为10进制
            m = cin.nextBigInteger(b);
            BigInteger k = p.mod(m);//大数模
            String ans = k.toString(b);//将数字转化为字符串(b进制)
            System.out.println(ans);
        }
    }
}

2.Octal Fractions 题目链接
涉及内容:
|大小数|去尾零|

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
            String s = cin.nextLine();//读入一行字符串
            BigDecimal a = BigDecimal.ZERO;//读入大小数
            BigDecimal b = BigDecimal.ONE;
            for (int i = 2; i < s.length(); i++) {//处理小数点后的数字
                double d = s.charAt(i) - '0';//java中的字符串中字符获取s.charAt()
                b = b.divide(BigDecimal.valueOf(8));//大小数除
                a = a.add(b.multiply(BigDecimal.valueOf(d)));//ans加
            }
            System.out.println(s + " [8] = " + a.stripTrailingZeros() + " [10]");
            //a.stripTrailingZeros()去除末尾零
        }
    }
}

3.NUMBER BASE CONVERSION 题目链接
涉及内容:
|BigInteger与int转化|

import java.util.*;
import java.math.*;
public class Main{
    static int ctoi(char cc) {
        if (cc >= '0' && cc <= '9') return cc - '0';
        else if(cc >= 'A' && cc <= 'Z') return cc - 'A' + 10;
        else return cc - 'a' +36;
    }
    static char itoc(int now) {
        if (now >= 0 && now <= 9) return (char) (now + '0');//记得括号
        else if (now >= 10 && now <= 35) return (char) (now - 10 + 'A');
        else return (char) (now - 36 + 'a');
    }
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n;
        n = cin.nextInt();
        for (int i = 1; i <= n; i++) {
            int a = cin.nextInt();
            int b = cin.nextInt();
            String s = cin.next();
            BigInteger now = BigInteger.valueOf(0);
            BigInteger c = BigInteger.valueOf(1);
            for (int j = s.length()-1; j >= 0; j--) {
                now = now.add(c.multiply(BigInteger.valueOf(ctoi(s.charAt(j)))));
                c = c.multiply(BigInteger.valueOf(a));
            }
            String ans = "";
            while (!now.equals(BigInteger.valueOf(0))) {
                ans = itoc(now.mod(BigInteger.valueOf(b)).intValue()) + ans;//intValue()注意下
                now = now.divide(BigInteger.valueOf(b));
                //System.out.println(now);
            }
            if (ans.equals("")) ans = "0";空串判定
            System.out.println(a + " " + s);
            System.out.println(b + " " + ans);
            System.out.println("");
        }
    }
}