USACO Section 2.4: Fractions to Decimals

时间:2023-03-09 14:54:54
USACO Section 2.4: Fractions to Decimals

乍看题目感觉有难度,实际分析后其实是道简单题

 /*
 ID: yingzho1
 LANG: C++
 TASK: fracdec
 */
 #include <iostream>
 #include <fstream>
 #include <string>
 #include <map>
 #include <vector>
 #include <set>
 #include <algorithm>
 #include <stdio.h>
 #include <queue>
 #include <cstring>
 #include <cmath>
 #include <list>

 using namespace std;
 #define inf 10000000

 ifstream fin("fracdec.in");
 ofstream fout("fracdec.out");

 int N, D;
 vector<int> rec;
 set<int> recset;

 int GCD(int a, int b)
 {
    ) return a;
    return GCD(b,a%b);
 }

 string intToString(int n) {
     string ret;
     ) ";
     while (n) {
         );
         ret += tmp;
         n /= ;
     }
     reverse(ret.begin(), ret.end());
     return ret;
 }

 int main()
 {
     fin >> N >> D;
     int gcd = GCD(N, D);
     N /= gcd, D/= gcd;
     ) {
         fout << N << ".0" << endl;
         ;
     }
     int first = N / D;
     int second = N % D;
     string res;
     ;
     ) {
         )) {
             for (; againindex < rec.size(); againindex++) {
         //        cout << rec[againindex] << endl;
                 ) {
                     againindex++;
                     break;
                 }
             }
             break;
         }
         res = res + /D);
         rec.push_back(second*);
         recset.insert(second*);
       //  cout << "second: " << second << endl;
         second = second *  % D;
     }
     //cout << res << endl;
     //cout << againindex << endl;
     if (againindex) {
         res.insert(againindex-, "(");
         //cout << res << endl;
         res += ")";
     }
     string ret = intToString(first) + "." + res;
     //cout << ret;
     ; i < ret.size(); i++) {
         fout << ret[i];
          ==  && i > ) fout << endl;
     }
      != ) fout << endl;
     //if (first > 0) fout << first << "." << res << endl;
     //else fout << "0." << res << endl;

     ;
 }