Selaa lähdekoodia

高精度小数,数组模拟

zj 5 vuotta sitten
vanhempi
commit
888ca75b0c
1 muutettua tiedostoa jossa 30 lisäystä ja 0 poistoa
  1. 30 0
      C2.cpp

+ 30 - 0
C2.cpp

@@ -0,0 +1,30 @@
+#include <bits/stdc++.h>
+
+using namespace std;
+
+int main() {
+    int a, b, c;
+    while (cin >> a >> b >> c) {
+        cout << a / b << ".";
+        a %= b;
+        vector<int> d;
+        while (d.size() <= c) {
+            int r = 10 * a;
+            a = r % b;
+            r /= b;
+            d.push_back(r);
+        }
+        if (d.back() >= 5) {
+            int sc = 1;
+            for (int i = c - 1; sc > 0 && i >= 0; i--) {
+                d[i] += sc;
+                sc = d[i] / 10;
+                d[i] %= 10;
+            }
+        }
+        d.erase(d.end() - 1);
+        copy(d.begin(), d.end(), ostream_iterator<int>(cout, ""));
+        cout << endl;
+    }
+    return 0;
+}