- // C = A + B, A >= 0, B >= 0
- vector<int> add(vector<int> &A, vector<int> &B)
- {
- //大的数+小的数
- if (A.size() < B.size()) return add(B, A);
-
- vector<int> C;
- int t = 0;
- for (int i = 0; i < A.size(); i ++ )
- {
- t += A[i];
- if (i < B.size()) t += B[i];
- C.push_back(t % 10);
- t /= 10;
- }
-
- //判断最高位是否进位
- if (t) C.push_back(t);
- return C;
- }
- // C = A - B, 满足A >= B, A >= 0, B >= 0
- vector<int> sub(vector<int> &A, vector<int> &B)
- {
- vector<int> C;
- for (int i = 0, t = 0; i < A.size(); i ++ )
- {
- t = A[i] - t;
- if (i < B.size()) t -= B[i];
- C.push_back((t + 10) % 10);
- if (t < 0) t = 1;
- else t = 0;
- }
-
- //去除前导0
- while (C.size() > 1 && C.back() == 0) C.pop_back();
- return C;
- }
- // C = A * b, A >= 0, b >= 0
- vector<int> mul(vector<int> &A, int b)
- {
- vector<int> C;
-
- int t = 0;
- for (int i = 0; i < A.size() || t; i ++ )
- {
- if (i < A.size()) t += A[i] * b;
- C.push_back(t % 10);
- t /= 10;
- }
-
- //去除前导0
- while (C.size() > 1 && C.back() == 0) C.pop_back();
-
- return C;
- }
- // A / b = C ... r, A >= 0, b > 0
- vector<int> div(vector<int> &A, int b, int &r)
- {
- vector<int> C;
- r = 0;
- for (int i = A.size() - 1; i >= 0; i -- )
- {
- r = r * 10 + A[i];
- C.push_back(r / b);
- r %= b;
- }
- reverse(C.begin(), C.end());
-
- //去除前导0
- while (C.size() > 1 && C.back() == 0) C.pop_back();
- return C;
- }
给定两个正整数(不含前导 ),计算它们的和。
共两行,每行包含一个整数。
共一行,包含所求的和。
1 ≤ 整数长度 ≤ 100000
- 12
- 23
35
- #include
- #include
- #include
- #include
- #include
- using namespace std;
-
- vector<int> add(vector<int>& A, vector<int>& B) {
- if (A.size() < B.size()) return add(B, A);
-
- vector<int> C;
- int t = 0;
- for (int i = 0; i < A.size(); i++) {
- t += A[i];
- if (i < B.size()) t += B[i];
- C.push_back(t % 10);
- t /= 10;
- }
-
- if (t) C.push_back(t);
- return C;
- }
-
- int main() {
- string a, b;
- cin >> a >> b;
-
- vector<int> A, B;
-
- for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
- for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
-
- auto C = add(A, B);
-
- for (int i = C.size() - 1; i >= 0; i--) cout << C[i];
- cout << endl;
- return 0;
- }
给定两个正整数(不含前导 ),计算它们的差,计算结果可能为负数。
共两行,每行包含一个整数。
共一行,包含所求的差。
1 ≤ 整数长度 ≤ 100000
- 32
- 11
21
- #include
- #include
- #include
- #include
- #include
- using namespace std;
-
- bool cmp(vector<int>& A, vector<int>& B) {
- if (A.size() != B.size()) return A.size() > B.size();
- for (int i = A.size() - 1; i >= 0; i--)
- if (A[i] != B[i]) return A[i] > B[i];
- return true;
- }
-
- vector<int> sub(vector<int>& A, vector<int>& B) {
-
- vector<int> C;
- int t = 0;
- for (int i = 0; i < A.size(); i++) {
- t = A[i] - t;
- if (i < B.size()) t -= B[i];
- C.push_back((t + 10) % 10);
- if (t < 0) t = 1;
- else t = 0;
- }
-
- //去前导0
- while (C.size() > 1 && C.back() == 0) C.pop_back();
-
- return C;
- }
-
- int main() {
- string a, b;
- cin >> a >> b;
-
- vector<int> A, B;
-
- for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
- for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
-
- if (cmp(A, B))
- {
- auto C = sub(A, B);
- for (int i = C.size() - 1; i >= 0; i--) cout << C[i];
- }
- else
- {
- auto C = sub(B, A);
- cout << "-";
- for (int i = C.size() - 1; i >= 0; i--) cout << C[i];
- }
- cout << endl;
- return 0;
- }
给定两个非负整数(不含前导 ) 和 ,请你计算 的值。
共两行,第一行包含整数 ,第二行包含整数 。
共一行,包含 的值。
1 ≤ A的长度 ≤100000,
0 ≤ B ≤10000
- 2
- 3
6
- #include
- #include
- #include
- #include
- #include
- using namespace std;
-
- vector<int> mul(vector<int>& A, int b) {
-
- vector<int> C;
- int t = 0;
- for (int i = 0; i < A.size() || t; i++) {
- if (i < A.size())t += A[i] * b;
- C.push_back(t % 10);
- t /= 10;
- }
-
- //去前导0
- while (C.size() > 1 && C.back() == 0) C.pop_back();
- return C;
- }
-
- int main() {
- string a;
- int b;
- cin >> a >> b;
-
- vector<int> A;
-
- for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
-
- auto B = mul(A, b);
-
- for (int i = B.size() - 1; i >= 0; i--) cout << B[i];
- cout << endl;
- return 0;
- }
给定两个非负整数(不含前导 ) ,,请你计算 的商和余数。
共两行,第一行包含整数 ,第二行包含整数 。
共两行,第一行输出所求的商,第二行输出所求余数。
1 ≤ A的长度 ≤100000,
0 ≤ B ≤10000,
B一定不为0
- 7
- 2
- 3
- 1
- #include
- #include
- #include
- #include
- #include
- using namespace std;
-
- vector<int> div(vector<int>& A, int b, int& r) {
-
- vector<int> C;
- r = 0;
-
- for (int i = A.size() - 1; i >= 0; i--) {
-
- r = r * 10 + A[i];
- C.push_back(r / b);
- r %= b;
- }
-
- reverse(C.begin(), C.end());
- while (C.size() > 1 && C.back() == 0) C.pop_back();
- return C;
- }
-
- int main() {
- string a;
- int b, r;
- cin >> a >> b;
-
- vector<int> A;
-
- for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
-
- auto B = div(A, b, r);
-
- for (int i = B.size() - 1; i >= 0; i--) cout << B[i];
- cout << endl << r << endl;
- return 0;
- }