Hdu 1717 decimal score 2

Source: Internet
Author: User

Cyclic decimal fraction ① pure cyclic decimal fraction, the numerator is the number of digits in a circular section, the number of the denominator is 9, and the number of 9 is equal to the number of The number of the cycle section, which can be an approximate score. Example 1: Convert 0.7 into a score. 0.7 × 10 = 7.777 ...... ① 0.7 = 0.777 ...... ② 0.7 × 9 = 7 from ①-② so 0.7 = 7/9 Example 2: Convert 5.1 2 3 into a score. 0.123 × 1000 = 123.123123 ...... ① 0.1 2 3 = 0.123123 ...... ② From ① To ② :.. 0.1 2 3 × 999 = 123 so .. 0.1 2 3 = 123/999 ② the number of digits in the fractional part before the second circular section, subtract the number composed of digits in the non-circular part of the decimal part and get the difference as the numerator. the first digit of the denominator is 9, the last number is, which is the same as the number of digits in a circular section. The number of digits 0 is the same as that in the non-circular section. For example, convert 0.478 to a score. 0.478 × 1000 = 478.7878 ...... ① 0.478 × 10 = 4.7878 ...... ② From ①-②: 0.478 × 990 = 474 so 0.478 = 474/990 For example: Convert 0.421 to score 0.421 × 1000 = 421.11111 ...... ① 0.421 × 100 = 42.11111 ...... ② From ①-②: 0.421 × 900 = 421-42 = 379 so 0.421 = 379/900 ---------------------------------------------------------------- I have not asked questions for more than 10 days !!! All hands are born !!! This question is not very difficult. It is mainly about the skills to convert decimals into scores !!! Due to my carelessness, there is a place where the output numbers are not simplified, which leads to many mistakes )!!! You have to work hard !!!! -------------------------------------------------------------------- # Include <stdio. h> # include <string. h> _ int64 P (_ int64 a ,__ int64 B) {_ int64 t, r; if (a <B) {t = a; a = B; B = t ;}do {r = a % B; a = B; B = r ;}while (r! = 0); return a;} int main () {_ int64 T, len, I, flag, num1, num2, j, q, gcd, flag2, nu1, nu2, n1, n2; char str [50], str1 [50], str2 [50]; scanf ("% I64d", & T); while (T --) {scanf ("% s", str); flag = 0; len = strlen (str); if (str [len-1] = ') flag = 1; if (flag) {for (I = 0; I <len-1; I ++) if (str [I] = '(') break; q = 0; for (j = I + 1; j <len-1; j ++) {str1 [q] = str [j]; q ++;} num1 = 0; num2 = 0; for (j = 0; j <q; j ++) num1 = num1 * 10 + (str1 [j]-'0'); for (j = 0; j <q; J ++) num2 = num2 * 10 + 9; gcd = P (num1, num2); num1/= gcd; num2/= gcd;} q = 0; flag2 = 0; for (I = 2; I <len; I ++) {if (str [I] = '(') break; str2 [q] = str [I]; q ++;} nu1 = 0; nu2 = 1; for (I = 0; I <q; I ++) {nu1 = nu1 * 10 + (str2 [I]-'0'); nu2 = nu2 * 10;} if (nu1 = 0) {flag2 = 0; for (I = 0; I <q; I ++) num2 * = 10; q = 0;} if (q> = 1) {flag2 = 1; gcd = P (nu1, nu2); nu1/= gcd; nu2/= gcd;} if (flag2 &&! Flag) {gcd = P (nu1, nu2); nu1/= gcd; nu2/= gcd; printf ("% I64d/% I64d \ n", nu1, nu2 );} if (flag &&! Flag2) {gcd = P (num1, num2); num1/= gcd; num2/= gcd; printf ("% I64d/% I64d \ n", num1, num2 );} if (flag & flag2) {for (I = 0; I <q; I ++) num2 = num2 * 10; n2 = num2 * nu2; n1 = num1 * nu2 + nu1 * num2; gcd = P (n1, n2); n1/= gcd; n2/= gcd; printf ("% I64d/% I64d \ n", n1, n2) ;}} return 0 ;}

Related Article

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.