Acwing794高精度除法

题目

给定两个非负整数(不含前导 00) A,B请你计算 A/B 的商和余数。

输入格式

共两行,第一行包含整数 A,第二行包含整数 B

输出格式

共两行,第一行输出所求的商,第二行输出所求余数。

数据范围

1≤A的长度≤100000 1≤B≤10000 B 一定不为 00

输入样例:

复制代码
7
2

输出样例:

复制代码
3
1

代码

java 复制代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    static int yu;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        int[] a = new int[s.length()];
        for (int i = 0 ;i < s.length();i ++) {
            a[i] = s.charAt(i) - '0';
        }
        int b = in.nextInt();
        Integer[] c = div(a, b);
        for (int i = 0;i < c.length;i ++) {
            System.out.print(c[i]);
        }
        System.out.println();
        System.out.println(yu);
    }

    private static Integer[] div(int[] a, int b) {
        List<Integer> sc = new ArrayList<>();
        int r = 0;
        for (int i =0;i < a.length;i ++) {
            r = r * 10 + a[i];
            sc.add(r / b);
            r = r % b;
        }
        yu = r;
        while (!sc.isEmpty() && sc.get(0) == 0) {
            sc.remove(0);
        }
        if(sc.isEmpty())    sc.add(0);
        return sc.toArray(sc.toArray(new Integer[0]));
    }
}
相关推荐
名字还没想好☜4 分钟前
Go slice 的 append 陷阱:共享底层数组导致的数据串改
开发语言·后端·golang·go·slice
诸神缄默不语7 分钟前
FastAPI后端配置CORS中间件支持浏览器跨域访问
后端·fastapi
Fanta丶8 分钟前
14.Activiti7 网关 排他网关ExclusiveGateway、并行网关ParallelGateway
后端
明月_清风16 分钟前
robots.txt 完全指南:从入门到精通
后端·爬虫
jvmind_dev17 分钟前
Java GC 实战指南(番外篇):被忽视的隐形杀手 —— Class Unloading 如何拖垮 GC
java·后端
明月_清风24 分钟前
从零写一个"像人"的爬虫:反爬攻防实战指南
后端·爬虫
贰先生26 分钟前
Xiuno BBS 审计之问题13:管理员发帖 doctype=0 时存储型 XSS
后端
贰先生43 分钟前
Xiuno BBS 审计之问题12:HTTPS 请求关闭 SSL 证书校验,存在中间人攻击风险
后端
HZ_YZ1 小时前
清理服务器硬盘脚本
后端
布朗克1681 小时前
Go 入门到精通-16-字符串深入
开发语言·后端·golang·字符串