#GOBJ503L. GESP 5级客观题|高精度运算|课堂讲解
GESP 5级客观题|高精度运算|课堂讲解
GESP 5级客观题|高精度运算|课堂讲解
考试频率:高频。本卷共 2 题。
- 下面的C++代码使用数组模拟整数加法,可以处理超出大整数范围的加法运算。横线处应填入代码是()。
vector<int> operator +(vector<int> a, vector<int> b){
vector<int> c;
int t = 0;
for (int i = 0; i < a.size() || i < b.size(); i++){
if (i < a.size()) t = t + a[i];
if (i < b.size()) t = t + b[i];
________
}
if (t) c.push_back(t);
return c;
}
{{ select(1) }}
push_back(t % 10), t = t % 10;push_back(t / 10), t = t % 10;push_back(t / 10), t = t / 10;push_back(t % 10), t = t / 10;
- 下面的代码片段用于将两个高精度整数进行相加。请在横线处填入( ),使其能正确实现相应功能。
string add(string num1, string num2) {
string result;
int carry = 0;
int i = num1.size() - 1, j = num2.size() - 1;
while (i >= 0 || j >= 0 || carry) {
int x = (i >= 0) ? num1[i--] - '0' : 0;
int y = (j >= 0) ? num2[j--] - '0' : 0;
int sum = x + y + carry;
carry = sum / 10;
_______________________________________
}
return result;
}
{{ select(2) }}
result = to_string(sum % 10) + result;result = to_string(carry % 10) + result;result = to_string(sum / 10) + result;result = to_string(sum % 10 + carry) + result;