Processing of input and output details, usaco fractions to Decimals

Source: Internet
Author: User

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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.