AlgorithmIt's still very simple. I have learned all about Division arithmetic in elementary school,BecauseIsThe remainder of each operation of N/D can only be 0 .. d-1. If there is a remainder before it appears (in decimal places), it is certain that there is a circular section between it and the previous operator. In this way, you can use the basic marking method.
Therefore, it is troublesome to handle the output, and to add parentheses. In addition, each line must output only 76 characters. Trouble! I'm depressed twice.
/* ID: fairyroadtask: fracdeclang: C ++ */# include <fstream> # include <vector> using namespace STD; ifstream fin ("fracdec. in "); ofstream fout (" fracdec. out "); bool existed [100001]; size_t index [100001]; vector <int> remainder; inline size_t countp (INT num) {If (! Num) return 1; int res = 0; while (Num) ++ res, num/= 10; return res;} int main () {int N, D; fin> N> D; int f = 0, CNT = 0; bool flag = true; // whether it can be divisible, related to parentheses if (n> = D) {f = N/d; n = n % d;} while (! Existed [N]) {existed [N] = true; index [N] = CNT ++; remainder. push_back (N * 10/d); If (n * 10) % d = 0) {flag = false; break;} n = (N * 10) % d;} fout <F <'. '; size_t charnum = countp (f) + 1; for (size_t I = 0; I <remainder. size (); ++ I) {If (charnum = 76) fout <Endl, charnum = 0; if (I = index [N] & flag) + + charnum, fout <'('; fout <remainder [I]; ++ charnum;} If (FLAG) fout <')'; fout <Endl; return 0 ;}