ccbeginner

UVa Q202

Jan 19th, 2020
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //UVa Q202
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define int unsigned
  5.  
  6. int32_t main(){
  7.     int n,m;
  8.     while(cin >> n >> m){
  9.         cout << n << '/' << m << " = " << n/m << '.';
  10.         n %= m;
  11.         vector<int> ans;
  12.         int rec[3001];
  13.         for(int i = 0; i < m; ++i)rec[i] = 1e9;
  14.         int cut = 1e9;
  15.         int cnt = 0;
  16.         rec[n] = 0;
  17.         while(n != 0 && cut == 1e9){
  18.             ++cnt;
  19.             n *= 10;
  20.             ans.emplace_back(n / m);
  21.             n %= m;
  22.             if(rec[n] != 1e9)cut = rec[n];
  23.             rec[n] = cnt;
  24.         }
  25.         if(n == 0)cut = ans.size();
  26.         for(int i = 0; i < cut; ++i)cout << ans[i];
  27.         ///////////////////////////////////////////
  28.         cout << '(';
  29.         for(int i = cut; i < min(cut+50,(int)ans.size()); ++i)cout << ans[i];
  30.         if(cut >= ans.size())cout << 0;
  31.         if(ans.size() > cut+50)cout << "...";
  32.         cout << ')' << '\n';
  33.         ///////////////////////////////////////////
  34.         int len = (n == 0)? 1 : ans.size()-cut;
  35.         cout << "   " << len << " = number of digits in repeating cycle\n\n";
  36.     }
  37.     return 0;
  38. }
RAW Paste Data