Explorar o código

大数加法:模拟

zj %!s(int64=5) %!d(string=hai) anos
pai
achega
c2722903ad
Modificáronse 1 ficheiros con 24 adicións e 0 borrados
  1. 24 0
      B.cpp

+ 24 - 0
B.cpp

@@ -0,0 +1,24 @@
+#include<bits/stdc++.h>
+
+using namespace std;
+
+int main() {
+    string a, b;
+    cin >> a >> b;
+    reverse(a.begin(), a.end());
+    reverse(b.begin(), b.end());
+    int n = max(a.size(), b.size());
+    while (a.size() < n)a.push_back('0');
+    while (b.size() < n)b.push_back('0');
+    string c(n, '0');
+    int sc = 0;
+    for (int i = 0; i < n; i++) {
+        int temp = sc + a[i] - '0' + b[i] - '0';
+        sc = temp / 10;
+        c[i] = '0' + temp % 10;
+    }
+    if (sc)c.push_back(sc + '0');
+    reverse(c.begin(), c.end());
+    cout << c << endl;
+    return 0;
+}