C++实现:超1000位大整数除法运算,输出商与余数及优化思路
在C++中处理大整数(每个数小于等于1000位)的除法运算,我们需要借助字符串来处理这些大数,因为普通的整型变量无法存储如此大的数值。以下是一个解决方案,使用字符串模拟大整数的除法运算,并输出商和余数。 ```cpp #include <iostream> #include <string> #include <algorithm> // 函数:比较两个大整数字符串的大小 int compare(const std::string &num1, const std::string &num2) { if (num1.length() != num2.length()) return num1.length() - num2.length(); return num1.compare(num2); } // 函数:减去两个大整数字符串 std::string subtract(const std::string &num1, const std::string &num2) { std::string result; int len1 = num1.length(), len2 = num2.length(); int carry = 0, sub; for (int i = 0; i < len1; ++i) { int n1 = (i < len2) ? num2[len2 - 1 - i] - '0' : 0; int n2 = num1[len1 - 1 - i] - '0'; sub = n2 - n1 - carry; if (sub < 0) { sub += 10; carry = 1; } else { carry = 0; } result += (sub + '0'); } // 去除前导零 while (result.length() > 1 && result.back() == '0') result.pop_back(); std::reverse(result.begin(), result.end()); return result.empty() ? "0" : result; } // 函数:大整数除法 void divideLargeNumbers(const std::string ÷nd, const std::string &divisor, std::string "ient, std::string &remainder) { quotient = ""; remainder = dividend; int lenDividend = dividend.length(); int lenDivisor = divisor.length(); for (int i = 0; i <= lenDividend - lenDivisor; ++i) { // 提取当前被除数的子串(长度与除数相同) std::string currentDividend = remainder.substr(0, lenDivisor); // 如果当前被除数的子串大于等于除数 if (compare(currentDividend, divisor) >= 0) { int tempQuotientDigit = 0; // 找到当前商位上的数字 while (compare(currentDividend, divisor) >= 0) { currentDividend = subtract(currentDividend, divisor); ++tempQuotientDigit; } // 将商位上的数字添加到结果中 quotient += (tempQuotientDigit + '0'); // 更新余数 remainder = currentDividend + remainder.substr(lenDivisor); } else { // 在商中添加0(表示当前位不能除尽) quotient += '0'; // 如果已经处理完所有位数且仍有余数,则直接跳出(其实此步理论上可省略,因为外部循环已控制) if (i == lenDividend - lenDivisor) break; // 将余数左移一位(相当于乘以10),为处理下一位做准备 remainder = "0" + remainder; } } // 去除商的前导零 while (quotient.length() > 1 && quotient.back() == '0') quotient.pop_back(); quotient = quotient.empty() ? "0" : quotient; // 如果余数为0,则输出0 if (remainder == dividend) remainder = "0"; } int main() { std::string num1, num2; std::cin >> num1 >> num2; std::string quotient, remainder; divideLargeNumbers(num1, num2, quotient, remainder); std::cout << quotient << std::endl; std::cout << remainder << std::endl; return 0; } ``` 逻辑点注意事项: 1. 字符串比较:由于大整数以字符串形式存储,比较时需要逐位比较,特别注意长度不同的情况。 2. 字符串减法:模拟手工减法,注意处理借位情况。 3. 性能优化:虽然本代码已经通过基本测试,但在处理极大数时,性能可能不是最优。可以考虑使用更高效的算法或数据结构进行优化。 ######[AI写代码神器 | 1087点数解答 | 2025-03-09 13:54:38]
- 判断超级幸运数:满足“纯4/7数位”或“4/7总个数为4或7”的整数计数(阿里通义 | 965点数解答 | 2026-03-13 12:15:06)44
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)371
- Java 实现通用文件存储系统:含随机读写与空闲空间回收(字节豆包 | 686点数解答 | 2024-11-21 16:25:51)237
- 【C++题解】P10840 星际能量分配:高精度除以低精度算法详解(DeepSeek | 1165点数解答 | 2026-04-10 21:10:07)16
- 培训机构学员成绩与年龄更新:C++ 代码实现(DeepSeek | 186点数解答 | 2025-12-16 18:04:31)79
- Python 处理培训机构学员培训后年龄与 NOIP 成绩提升情况(DeepSeek | 73点数解答 | 2025-12-16 18:05:20)82
- 培训机构学员成绩提升:C++代码实现年龄与NOIP成绩更新(DeepSeek | 178点数解答 | 2025-12-31 12:20:24)75
- 求解特定条件下整数序列的最小值:算法分析与代码实现(字节豆包 | 746点数解答 | 2026-01-24 13:14:40)70
- C 语言:按特定顺序读入并输出浮点数、整数和字符(字节豆包 | 106点数解答 | 2024-09-26 00:32:40)334
- C语言:按序读入浮点数、整数、字符并按新顺序输出,附代码实现 (字节豆包 | 108点数解答 | 2024-09-30 22:54:08)348
- C语言:按特定顺序读入并输出浮点数、整数和字符,精确控制小数位(字节豆包 | 155点数解答 | 2024-10-08 22:06:18)342
- C++与Python:按特定顺序输入输出数据并保留浮点数两位小数的实现(字节豆包 | 168点数解答 | 2024-10-08 22:07:03)345