日常——高精度除法

题目描述

给定两个非负整数A,B,请你计算 A / B的商和余数。

输入格式

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

输出格式

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

数据范围

1 ≤ A . l e n g t h ≤ 100000 1 \le A.length \le 100000 1A.length100000
0 ≤ B ≤ 10000 0 \le B \le 10000 0B10000

输入样例:

7
2

输出样例:

3
1

Solution

package cn.zxy.test;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

/**
 * 高精度除法
 * 7
 * 2
 *
 * 3
 * 1
 */
public class highPrecisionDivision {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String sa = sc.next();
        int la = sa.length();
        int[] a = new int[la];
        for(int i = 0, j = 0; i < la; i++, j++) a[i] = sa.charAt(j) - '0';
        int b = sc.nextInt();
        Deque<Integer> c = new ArrayDeque<>();
        // 商用 c 存储,返回余数
        int t = div(a, b, c);
        while(!c.isEmpty()) System.out.print(c.pollFirst());
        System.out.print("\n" + t);
    }

    private static int div(int[] a, int b, Deque<Integer> c) {
        int t = 0;
        for(int i = 0; i < a.length; i++){
            t = t * 10 + a[i];
            c.addLast(t / b);
            t = t % b;
        }
        while(c.size() > 1 && c.peekFirst() == 0) c.pollFirst();
        return t;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值