Decimal fraction of HDU-1717 2

Source: Internet
Author: User

This question requires that decimals be passed into scores. This process is a pure mathematical operation. It is probably an Olympics question in elementary school .. Wow ....

If it is a common score, it can be said that it is the number of the same multiplication to the power of 10 ^ X, and then the approximate score is enough.

For example, 0.234 --> 234/1000 --> 117/500

If it is a pure cyclic decimal, it is necessary to use the equation to solve it.

For example, if x = 0. (1234) is set to 0. (1234), returns X-x = 1234 to the decimal point of the loop, and returns x = 1234/9999, which is equal to 1234/9999 in the approximate minute.

If this is the case, 0.24 (765) can clearly understand that this is the integrated version of the first two. Separate calculation and then calculate the total score. Here we still use the equation to solve the problem.

For example, if X = 0.24 (765), 100000x-100x = 0.24-24, X = 765 is obtained.

CodeAs follows:

# Include <stdio. h> # include <stdlib. h> # include <math. h> # include <string. h> # include <time. h> # include <ctype. h> char num [20]; int gcd (int x, int y) {While (x) {int c = x; X = Y % x; y = C ;} return y;} int getmo (INT len1, int len2) {int sum = 9; For (INT I = 2; I <= len2; ++ I) {sum = sum * 10 + 9;} For (INT I = 1; I <= len1; ++ I) {sum * = 10;} return sum ;} int _ POW (int x) {int sum = 1; for (INT I = 1; I <= x; ++ I) {sum * = 10;} return sum;} int getson (INT sp, int so) {return SP-So;} int main () {int N; scanf ("% d", & N); While (n --) {int so, SP, Len, len1, len2, point, son, Mo; scanf ("% s", num); Len = strlen (Num); If (Num [Len-1]! = ') {Mo = _ POW (LEN-2); son = atoi (Num + 2); int e = gcd (son, Mo ); // obtain the maximum approx. printf ("% d/% d \ n", son/e, Mo/E); continue ;} num [Len-1] = '\ 0'; point = strchr (Num,' (')-num; len1 = point-2; // non-cyclic String Length len2 = strlen (Num + point + 1); // cyclic String Length mo = getmo (len1, len2 ); // obtain the denominator so = atoi (Num + 2); SP = atoi (Num + point + 1 ); // convert the cyclic and non-cyclic strings into numbers sp = So * _ POW (len2) + SP; son = getson (SP, so); int e = gcd (son, mo); printf ("% d/% d \ n", son/e, Mo/E);} 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.