Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <bits/stdc++.h>
- #include <string>
- using namespace std;
- string solve(int a, int b)
- {
- string headString = "";
- if (a == 0)
- {
- return "0";
- }
- if ((a < 0 && b > 0) || (a > 0 && b < 0))
- {
- headString += "-";
- }
- int temp = abs(a / b);
- headString += to_string(temp);
- int c = abs(a % b);
- if (c == 0)
- {
- return headString;
- }
- headString += ".";
- string backString = "";
- map<int, int> m;
- while (c != 0)
- {
- if (m.find(c) != m.end())
- {
- int index = m[c];
- backString.insert(index, "(");
- backString += ")";
- break;
- }
- m[c] = backString.length();
- c *= 10;
- backString += to_string(c / b);
- c %= b;
- }
- return headString + backString;
- }
- int main()
- {
- int a, b;
- cin >> a >> b;
- cout << solve(a, b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement