大整数除法

大整数除法:

#include <iostream>
#include <cstring>
using namespace std;

int compare(int *, int *);
void sub(int *, int *);
void clear_zero(int *);
void output(int *); 

int main() {
	char s1[1005], s2[1005];
    int n1[1005] = {0}, n2[1005] = {0}, temp[1005] = {0};
    int ans[1005] = {0};
    cin >> s1 >> s2;
    // 输入 n1被除数  n2 除数
    n1[0] = strlen(s1), n2[0] = strlen(s2);
    ans[0] = n1[0];
    for (int i = 1; i <= n1[0]; i++) {
        n1[i] = s1[i - 1] - '0';
    }
    for (int i = 1; i <= n2[0]; i++) {
        n2[i] = s2[i - 1] - '0';
    }
    /*
    // 减法调试
    sub(n1, n2);
    cout << "减法:" << endl;
    for (int i = 1; i <= n1[0]; i++) {
        cout << n1[i];
    }
    cout << endl;
    */
    // 除法
    for (int i = 1; i <= n1[0]; i++) {
        // temp 临时被除数  n2 除数
        // step 1: 给temp赋值
        temp[++temp[0]] = n1[i]; // 模拟除法 temp 补充一位
        clear_zero(temp);
        // cout << "temp 补位" << endl;
        // output(temp);
        if (compare(temp, n2) < 0 ) { //可以使用strcmp但是数组要定义为字符串类型,因为不涉及乘法是可以用字符串类型的。        
            continue;                 // ans[i] = 0
        }

        // 使用大整数减法
        while(compare(temp, n2) >= 0) {
            //执行 temp = temp - n2 操作, 更新temp; temp也是最后余数
            sub(temp, n2);
            ans[i]++;
        }
    }
    cout << "商是:" << endl;   
    clear_zero(ans);
    output(ans);
    cout << "余数是:" << endl;
    output(temp);
	return 0;
}

void sub(int *temp, int *n2) {
    /*
    if (compare(temp, n2) < 0) {
        return ;
    }
    */
    // temp >= n2
    for (int i = temp[0], j = n2[0]; i > 0 && j > 0; i--, j--) {
        // cout << temp[i] << " - " << n2[j] << " = " << temp[i] - n2[j] << endl;
        temp[i] -= n2[j];
    }
    // 借位
    for (int i = temp[0]; i > 1; i--) {
        if (temp[i] < 0) {
            temp[i - 1] += temp[i] / 10 - 1; // 这里temp[i]是负数
            temp[i] = 10 + temp[i] % 10;
        }
    } 
    clear_zero(temp);
    return ;
}

void clear_zero(int *temp) {
    int ind = 0; 
    for (int i = 1; i <= temp[0]; i++, ind++) {
        if (temp[i] != 0) break;
    }
    // 去掉前面空位的0 
    for (int i = 1, j = ind + 1; j <= temp[0]; i++, j++) {
        temp[i] = temp[j];
    }
    if (temp[0] == ind) {
        temp[0] = 1;
    } else {
        temp[0] -= ind;
    }
    return ;
}

int compare(int *n1, int *n2) {
    if (n1[0] > n2[0]) return 1;
    else if (n1[0] < n2[0]) return -1;
    else {  // n1[0] == n2[0]
        for (int i = 1; i <= n1[0]; i++) {
            if (n1[i] > n2[i]) return 1;
            else if (n1[i] < n2[i]) return -1;
        }
        return 0;
    }
}

void output(int *temp) { 
    for ( int i = 1; i <= temp[0]; i++ ) {
        cout << temp[i];
    }
    cout << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值