3 条题解

  • 0
    @ 2025-2-9 0:31:34

    代码3:直接调用高精度减法的方法:

    #include <bits/stdc++.h>
    using namespace std;
    // 高精度减法:计算 a - b,返回结果
    string subBig(string a, string b) {
    	string res;
    	int carry = 0;
    	int n = a.size(), m = b.size();	
    	// 从低位到高位逐位相减
    	for (int i = n - 1, j = m - 1; i >= 0 || j >= 0; i--, j--) {
    		int x = (i >= 0) ? (a[i] - '0') : 0;
    		int y = (j >= 0) ? (b[j] - '0') : 0;
    		int diff = x - y - carry;
    		if (diff < 0) {
    			diff += 10;
    			carry = 1;
    		} else {
    			carry = 0;
    		}
    		res.push_back(diff + '0');
    	}	
    	// 去掉结果的前导零
    	reverse(res.begin(), res.end());
    	res.erase(0, res.find_first_not_of('0'));
    	if (res.empty()) res = "0";
    	
    	return res;
    }
    // 大整数除法,直接调用高精度减法计算商和余数
    pair<string, string> divBig(const string& a, const string& b) {
    	string shang = "0", ys = a;  // 商初始化为 0, 余数初始化为被除数a
    	int m = b.size();	
    	// 如果被除数小于除数,商为 0,余数为被除数
    	if (ys.size() < m || (ys.size() == m && ys < b)) {
    		return {"0", ys};
    	}	
    	// 逐次调用高精度减法
    	int cnt = 0;  // 用于计算商的当前位
    	while (ys.size() > m || (ys.size() == m && ys >= b)) {
    		ys = subBig(ys, b);  // 每次从余数中减去除数
    		cnt++;  // 商的当前位加 1
    		shang = to_string(cnt);  // 更新商
    	}	
    	return {shang, ys};
    }
    int main() {
    	string a, b;
    	cin >> a;
    	cin >> b;	
    	auto res = divBig(a, b);
    	cout << res.first << endl;  // 输出商
    	cout << res.second << endl; // 输出余数	
    	return 0;
    }
    
    
    • 0
      @ 2025-2-8 23:59:43

      代码2:

      #include <bits/stdc++.h>
      using namespace std;
      // 高精度减法:计算 a - b,返回结果
      string subBig(string a, string b) {
      	string res;
      	int carry = 0;
      	int n = a.size(), m = b.size();	
      	// 从低位到高位逐位相减
      	for (int i = n - 1, j = m - 1; i >= 0 || j >= 0; i--, j--) {
      		int x = (i >= 0) ? (a[i] - '0') : 0;
      		int y = (j >= 0) ? (b[j] - '0') : 0;
      		int diff = x - y - carry;
      		if (diff < 0) {
      			diff += 10;
      			carry = 1;
      		} else {
      			carry = 0;
      		}
      		res.push_back(diff + '0');
      	}
      	
      	// 去掉结果的前导零
      	reverse(res.begin(), res.end());
      	res.erase(0, res.find_first_not_of('0'));
      	if (res.empty()) res = "0";
      	
      	return res;
      }
      // 大整数除法,返回商和余数
      pair<string, string> divBig(const string& a, const string& b) {
      	string shang, ys; // shang: 商, ys: 余数
      	int n = a.size(), m = b.size();
      	
      	// 如果被除数小于除数,商为 0,余数为被除数
      	if (n < m || (n == m && a < b)) {
      		return {"0", a};
      	}	
      	// 逐位计算商和余数
      	ys = "";
      	for (int i = 0; i < n; i++) {
      		ys += a[i]; // 将当前位加入余数
      		int cnt = 0; // 当前位的商
      		while (ys.size() > m || (ys.size() == m && ys >= b)) {
      			ys = subBig(ys, b); // 调用高精度减法
      			cnt++;
      		}
      		shang.push_back(cnt + '0');
      	}	
      	// 去掉商的前导零
      	shang.erase(0, shang.find_first_not_of('0'));
      	if (shang.empty()) shang = "0";	
      	// 去掉余数的前导零
      	ys.erase(0, ys.find_first_not_of('0'));
      	if (ys.empty()) ys = "0";
      	return {shang, ys};
      }
      
      int main() {
      	string a, b;
      	cin >> a;
      	cin >> b;	
      	auto res = divBig(a, b);
      	cout <<res.first << endl;
      	cout <<res.second << endl;
      	
      	return 0;
      }
      
      • 0
        @ 2025-2-5 22:22:37
        #include <bits/stdc++.h>
        using namespace std;
        
        vector<int> a, b, c;
        
        int cmp(const vector<int>& a, const vector<int>& b) { // 比较 a 和 b
            if (a.size() > b.size()) return 1;
            if (a.size() < b.size()) return -1;
            for (int i = a.size() - 1; i >= 0; --i) {
                if (a[i] > b[i]) return 1;
                if (a[i] < b[i]) return -1;
            }
            return 0;
        }
        
        void sub(vector<int>& a, const vector<int>& b) { // 计算 a = a - b
            int carry = 0;
            for (int i = 0; i < a.size(); i++) {
                if (i < b.size()) {
                    a[i] -= b[i] + carry;
                    if (a[i] < 0) {
                        a[i] += 10;
                        carry = 1;
                    } else {
                        carry = 0;
                    }
                } else if (carry) {
                    a[i] -= carry;
                    if (a[i] < 0) {
                        a[i] += 10;
                        carry = 1;
                    } else {
                        carry = 0;
                    }
                }
            }
            while (a.size() > 1 && a.back() == 0) a.pop_back(); // 删除前导零
        }
        
        int main() {
            string s1, s2;
            cin >> s1 >> s2;
            
            // 转换字符串为数字,保存在动态数组 a 和 b 中
            a.clear();
            b.clear();
            
            for (int i = s1.size() - 1; i >= 0; --i) a.push_back(s1[i] - '0');
            for (int i = s2.size() - 1; i >= 0; --i) b.push_back(s2[i] - '0');
        
            c.clear();
            c.push_back(0); // 初始化商的高位为 0
            for (int i = a.size() - 1; i >= 0; --i) {
                vector<int> temp = b;
                while (cmp(a, temp) >= 0) {  // 如果 a >= b
                    c[0]++;
                    sub(a, b);
                }
            }
            
            // 输出商
            if (c.size() == 1 && c[0] == 0) cout << "0" << endl;
            else {
                for (int i = c.size() - 1; i >= 0; --i) cout << c[i];
                cout << endl;
            }
        
            // 输出余数
            if (a.size() == 1 && a[0] == 0) cout << "0" << endl;
            else {
                for (int i = a.size() - 1; i >= 0; --i) cout << a[i];
                cout << endl;
            }
        
            return 0;
        }
        
        
        • 1

        信息

        ID
        786
        时间
        1000ms
        内存
        256MiB
        难度
        9
        标签
        递交数
        17
        已通过
        4
        上传者