Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: mickyta1
- TASK: fracdec
- LANG: C++
- */
- #include <bits/stdc++.h>
- using namespace std;
- #define f first
- #define s second
- typedef pair<int, int> pii;
- const int N = 1e5 + 5;
- deque<pii> cycle;
- vector<pii> noCycle;
- string str;
- bool visited[N];
- void printStr(){
- int upb = ((int)str.size() + 75) / 76;
- for(int i = 0; i < upb; ++i){
- for(int j = 0; j < 76 && 76 * i + j < str.size(); ++j){
- cout << str[76 * i + j];
- }
- cout << '\n';
- }
- }
- int main(){
- freopen("fracdec.in", "r", stdin);
- freopen("fracdec.out", "w", stdout);
- int st, deno;
- scanf("%d%d", &st, &deno);
- if(st % deno == 0){
- cout << st / deno << ".0\n";
- fclose(stdin);
- fclose(stdout);
- return 0;
- }
- str += to_string(st / deno);
- str.push_back('.');
- st %= deno;
- while(true){
- if(st == 0){
- goto hasNoCycle;
- }
- int u = 10 * st / deno;
- if(visited[st]){
- while(!cycle.empty() && cycle.front().s != st){
- noCycle.push_back(cycle.front());
- cycle.pop_front();
- }
- goto hasCycle;
- }
- visited[st] = true;
- cycle.emplace_back(u, st);
- st = 10 * st % deno;
- }
- hasNoCycle:
- for(int i = 0; i < cycle.size(); ++i){
- str.push_back((char)(cycle[i].f + '0'));
- }
- printStr();
- fclose(stdin);
- fclose(stdout);
- return 0;
- hasCycle:
- for(int i = 0; i < noCycle.size(); ++i){
- str.push_back((char)(noCycle[i].f + '0'));
- }
- str.push_back('(');
- for(int i = 0; i < cycle.size(); ++i){
- str.push_back((char)(cycle[i].f + '0'));
- }
- str.push_back(')');
- printStr();
- fclose(stdin);
- fclose(stdout);
- return 0;
- }
Advertisement
RAW Paste Data
Copied
Advertisement