[Cyclic decimal nature] HDU 1717 -- decimal score 2

Source: Internet
Author: User

Question: Click to open the link

This question is a simulation. The general steps for decimal scores can be viewed in my previous blog. It is only divided by the power of the digits of 10, and then about a minute. This is also the case. It is complicated to process infinite decimal places.

There are three situations:

1. Pure cyclic decimal

2. Pure non-repeating decimal places

3. Mixed decimal places

The three transformations have their own nature. The conclusions are as follows (for the proof process, see Baidu)

1. If the number of decimal places is limited, divide the number after the decimal point by 10 (one digit ). 100 (double digit ). 1000 (three digits) and so on. 2. If it is an infinite repeating decimal point, divide the number of loops by 9, 99, 999 (same as above). 3. If it is a mixed repeating decimal point, if the number is a two-digit non-repeating number, one digit is divided by 990, the two digits are 9900, and the non-repeating decimal number is multiplied by 990 or 9900.
That is: The Numerator = the non-repeating part and the cyclic part are connected-the non-repeating part. Denominator = 99 .. (number of cycles) 0 .. (number of non-cyclic digits)

The rest is about simulating string extraction and other details. In addition, it is best to write the POW function by yourself, and sometimes the precision will be lost, resulting in WA.

# Include <iostream> # include <string> # include <cmath> # include <stdlib. h> using namespace STD; int gcd (int A, int B) {If (B = 0) return a; return gcd (B, A % B );} int lcm (int A, int B) {return (A * B)/gcd (a, B);} int my_pow (INT X, int N) {int res = 1; for (INT I = 1; I <= N; I ++) RES * = x; return res;} int main () {int testcase; CIN> testcase; for (int A = 0; A <testcase; A ++) {string tar; string integerstr, loopstr; int integer, loop; int loopsize = 0, Intsize = 0; CIN> tar; If (TAR. find (', 0) =-1) // first case, pure integer, excluding the cyclic section {for (INT I = 2; I <tar. size (); I ++) {integerstr + = tar [I]; intsize ++;} integer = atoi (integerstr. c_str (); int cm; CM = gcd (integer, my_pow (10, intsize); cout <integer/cm <"/" <my_pow (10, intsize)/cm <Endl;} else if (TAR [2] = '(') // In the second case, pure cyclic decimal point {for (INT I = 3; I <tar. size ()-1; I ++) {loopstr + = tar [I]; loopsize ++;} loop = atoi (loopstr. c_str (); int DIV = my_pow (10, loopsize) -1; int CM = gcd (loop, Div); cout <loop/cm <"/" <DIV/cm <Endl;} else if (TAR. find (', 0 )! =-1 | tar. Find (', 0 )! = 2) {int Pos; int INTF; int res; For (INT z = 0; Z <tar. size (); Z ++) {If (TAR [Z] = '(') Pos = z;} For (INT I = 2; I <Pos; I ++) {integerstr + = tar [I]; intsize ++ ;}for (Int J = POS + 1; j <tar. size ()-1; j ++) {loopstr + = tar [J]; loopsize ++;} string fenzistr; int fenzi; fenzistr = integerstr + loopstr; fenzi = atoi (fenzistr. c_str (); integer = atoi (integerstr. c_str (); loop = atoi (loopstr. c_str (); Res = fenzi-integer; string intfm; // For example, 0.32 (56). The original denominator is, and the number of 0 equals to the number of non-cyclic segments, 9 is equal to the number of cyclic nodes for (INT p = 0; P <loopsize; P ++) {intfm + = '9' ;}for (int o = 0; O <intsize; O ++) {intfm + = '0';} INTF = atoi (intfm. c_str (); int CM = gcd (Res, INTF); cout <Res/cm <"/" <INTF/cm <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.