• C++
  • C++实现高精度除以低精度的竖式模拟算法

  • @ 2025-9-21 14:00:29

下面我将为你介绍如何用C++实现高精度除以低精度的竖式模拟算法。这种方法不做任何优化,完全模拟手工竖式除法的过程,适合理解除法的基本原理。

算法思路:

  1. 用字符串存储高精度数(被除数)
  2. 用整数存储低精度数(除数)
  3. 从被除数的最高位开始,逐位进行除法运算
  4. 记录每一步的商和余数
  5. 处理前导零的情况

下面是具体实现代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// 高精度除以低精度,返回商和余数
pair<string, int> divide(string high, int low) {
    vector<int> quotient;  // 存储商的每一位
    int remainder = 0;     // 余数
    
    // 从最高位开始计算
    for (int i = 0; i < high.size(); i++) {
        // 当前位的值加上上一步的余数*10
        int current = remainder * 10 + (high[i] - '0');
        
        // 计算当前位的商
        int q = current / low;
        // 更新余数
        remainder = current % low;
        
        // 存储商的当前位
        quotient.push_back(q);
    }
    
    // 处理前导零
    int start = 0;
    while (start < quotient.size() && quotient[start] == 0) {
        start++;
    }
    
    // 转换为字符串
    string quotient_str;
    if (start == quotient.size()) {
        // 商为0的情况
        quotient_str = "0";
    } else {
        for (int i = start; i < quotient.size(); i++) {
            quotient_str += (quotient[i] + '0');
        }
    }
    
    return {quotient_str, remainder};
}

int main() {
    string dividend;  // 被除数(高精度)
    int divisor;      // 除数(低精度)
    
    cout << "请输入被除数(高精度整数): ";
    cin >> dividend;
    
    cout << "请输入除数(低精度整数): ";
    cin >> divisor;
    
    // 检查除数是否为0
    if (divisor == 0) {
        cout << "错误:除数不能为0!" << endl;
        return 1;
    }
    
    // 执行除法运算
    auto result = divide(dividend, divisor);
    
    // 输出结果
    cout << "商: " << result.first << endl;
    cout << "余数: " << result.second << endl;
    
    return 0;
}

代码解析:

  1. 函数divide接收一个表示高精度数的字符串和一个低精度整数,返回商(字符串)和余数(整数)的 pair。

  2. 算法核心步骤:

    • 初始化余数为0
    • 遍历被除数的每一位数字
    • 计算当前值:current = 余数 * 10 + 当前位数字
    • 计算当前位的商:q = current / 除数
    • 更新余数:remainder = current % 除数
    • 保存当前位的商
  3. 处理前导零:当商的前面有零时,需要将这些零去除,只保留有效数字。

  4. 特殊情况处理:当商全为零时,返回"0"而不是空字符串。

使用示例: 如果输入被除数"123456"和除数"7",程序会模拟竖式计算:

  • 1÷7=0余1
  • 12÷7=1余5
  • 53÷7=7余4
  • 44÷7=6余2
  • 25÷7=3余4
  • 46÷7=6余4

最终得到商"17636"和余数4。

这种方法完全模拟了人工竖式除法的过程,没有做任何优化,适合初学者理解高精度除法的基本原理。

1 条评论

  • @ 2025-9-21 14:28:12
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    // 高精度除以低精度,返回商和余数
    pair<string, int> divide(string high, int low) {
    	vector<int> quotient;  // 存储商的每一位
    	int remainder = 0;     // 余数
    	// 从最高位开始计算
    	for (int i = 0; i < high.size(); i++) {
    		// 当前位的值加上上一步的余数*10
    		int current = remainder * 10 + (high[i] - '0');
    		// 计算当前位的商
    		int q = current / low;
    		// 更新余数
    		remainder = current % low;
    		// 存储商的当前位
    		quotient.push_back(q);
    	}
    	// 处理前导零
    	int start = 0;
    	while (start < quotient.size() && quotient[start] == 0) {
    		start++;
    	}
    	// 转换为字符串
    	string quotient_str;
    	if (start == quotient.size()) {
    		// 商为0的情况
    		quotient_str = "0";
    	} else {
    		for (int i = start; i < quotient.size(); i++) {
    			quotient_str += (quotient[i] + '0');
    		}
    	}
    	return {quotient_str, remainder};//在 C++ 中,return {quotient_str, remainder}; 这种语法是可以返回一个 pair 对象的,这是 C++11 及以后标准支持的列表初始化(list initialization)语法。
    }
    int main() {
    	string dividend;  // 被除数(高精度)
    	int divisor;      // 除数(低精度)
    	cout << "请输入被除数(高精度整数): ";
    	cin >> dividend;
    	cout << "请输入除数(低精度整数): ";
    	cin >> divisor;
    	// 检查除数是否为0
    	if (divisor == 0) {
    		cout << "错误:除数不能为0!" << endl;
    		return 1;
    	}
    	// 执行除法运算
    	auto result = divide(dividend, divisor);
    	// 输出结果
    	cout << "商: " << result.first << endl;
    	cout << "余数: " << result.second << endl;
    	return 0;
    }
    
    
    
    • 1