Advertisement
SaberToaster

Proper A divide B i guess

Aug 19th, 2022 (edited)
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <bits/stdc++.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. string solve(int a, int b)
  9. {
  10.     string headString = "";
  11.     if (a == 0)
  12.     {
  13.         return "0";
  14.     }
  15.     if ((a < 0 && b > 0) || (a > 0 && b < 0))
  16.     {
  17.         headString += "-";
  18.     }
  19.     int temp = abs(a / b);
  20.     headString += to_string(temp);
  21.     int c = abs(a % b);
  22.     if (c == 0)
  23.     {
  24.         return headString;
  25.     }
  26.     headString += ".";
  27.     string backString = "";
  28.     map<int, int> m;
  29.     while (c != 0)
  30.     {
  31.         if (m.find(c) != m.end())
  32.         {
  33.             int index = m[c];
  34.             backString.insert(index, "(");
  35.             backString += ")";
  36.             break;
  37.         }
  38.         m[c] = backString.length();
  39.         c *= 10;
  40.         backString += to_string(c / b);
  41.         c %= b;
  42.     }
  43.     return headString + backString;
  44. }
  45.  
  46. int main()
  47. {
  48.     int a, b;
  49.     cin >> a >> b;
  50.     cout << solve(a, b);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement