HDU1717 -- decimal score 2

Source: Internet
Author: User

This question is to convert the input decimal point (possibly an infinite loop decimal point) into a fraction. At first I thought it was enumeration (never laugh at me), but I felt wrong,

So Baidu decimal to the score method, and then saw a variety of methods, the original is this, here I am using the decimal to the score of the set of public Method


Pure Loop

When 9 is used as the denominator, the number of cycles is 9, for example, 0.3. The number of cycles in 3 is 3 in 9, 0.654 in 654, and 999 in 654, the cycle of 9 is 9 to 9 (1), and so on.

Here are several examples of mixed loops. Example:Convert the mixed loop decimal point 0.228 to the score: Solution:0.228 ˙ = [(228/1000) + 8/9000)] = 228/(900 + 100) + 8/9000 = [(228/900)-(228/9000)] + (8/9000) = (228/900) + [(8/9000)-(228/9000)] = (228/900)-(22/900) = (228-22)/900 = 206/900 = 103/450; Example:Convert the mixed-loop decimal point 0.123*68 * into a score: solution: 0.123*68 * = (0.12368 + 0.00000*68 *) = (12368/100000) + (68/9900000) = [(12368/99000) -(12368/990000)] + (68/9900000) = (12368/99000) + [(68/9900000)-(12368/9900000)] = (12368/99000)-(12300/9900000) = (12368-123)/99000 FormulaUse 9 and 0 as the denominator. First, there are several cycle sections for a few 9, and then there are a few numbers that do not join the cycle plus a few 0, use the number after the decimal point minus the number not added to the cycle, such as the cycle of 0.43, 3, a single digit is not added to the cycle, then add a 0 after 9 as the denominator, use 43 minus 4 for molecules, get 39, 0.145, and 5 for the cycle of 9 plus 2 0 for denominator, then use 145 minus 14 for molecules, get 900 points of 131, the cycle of 0.549 and 49 is followed by 99 plus 1 0 as the denominator, 549 minus 5 as the numerator, and 990 to 545, and so on.

After reading the above, you will know how to convert decimal places into scores.

Then the code is implemented. If you understand the above method, the rest is actually string processing. In the end, do not forget to simply get the score, in fact, simplification is to find the maximum common divisor between the numerator and the denominator. After processing, the conversion will be OK!

Paste your own code!

# Include
 
  
# Include
  
   
Using namespace std; // calculates the maximum common approx. int gcd (int a, int B) {int t; if (a <B) {t = a; a = B; B = t;} while (B> 0) {t = B; B = a % B; a = t;} return a;} int main () {# ifdef LOCALfreopen ("input.txt", "r", stdin); # endifint T; cin> T; string str; while (T --) {cin> str; int primLength = 0, lastLength = 0, primValue = 0, lastValue = 0; bool flag = false; int fenzi = 1, fenmu = 1; // process the input decimal string for (int I = 2; I
   
    
= '0' & str [I] <= '9') {if (flag) {lastLength ++; lastValue = lastValue * 10 + (str [I]-'0');} else {primLength ++; primValue = primValue * 10 + (str [I]-'0') ;}}// process if (flag) for infinite loops and non-loops respectively) {int temp = primValue; int sum = 0; while (lastLength --) {temp = temp * 10; sum = sum * 10 + 9;} fenzi = temp + lastValue-primValue; fenmu = sum; while (primLength --) {fenmu = fenmu * 10;} // output cout <fenzi/gcd (fenzi, fenmu) <"/" <fenmu/gcd (fenzi, fenmu) <endl;} else {while (primLength --) {fenmu * = 10 ;} // output cout <primValue/gcd (fenmu, primValue) <"/" <fenmu/gcd (fenmu, primValue) <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.