Decimal addition time limit: 1000 MS | memory limit: 65535 kb difficulty: 4
-
Description
-
Give you two decimal places. Can you calculate their sum?
You will say so easy.
However, what if some of these decimals are infinite cyclic decimals?
An infinite repeating decimal point consists of three parts: integer part, non-repeating fractional part, and fractional repeating part.
For example:
The three parts of 1.2 (34) are 1 2 34.
2. the integer part of (04) is 2, the decimal part does not exist, and the decimal part is 04
The integer part of 2.4 is 2, the fractional non-repeating part is 4, and the fractional repeating part does not exist.
Generally, the fractional repeating part is at the end of the decimal point.
Now, calculate the sum of two infinitely repeating decimal places
-
Input
-
Enter an integer N in the first line, indicating that there are n rows of test data (1 <= n <= 100)
Input six strings S1, T1, R1, S2, T2, R2 in the second line, respectively, indicating the integer part of the first and second round decimal places. (If this part does not exist, enter $)
Integer part. The decimal part does not have a repeating part. The length of the repeating part cannot exceed 10 digits.
Each part of the input is a positive number.
-
Output
-
Outputs the sum of two numbers. Convert the output result to the simplest score (not in the form of a score ). (If it is an integer, It is output directly)
-
Sample Input
-
31 $ 3 2 $ 30 1 3 0 $ 62 03 $ 2 4 $
-
Sample output
-
11/34/5443/100
-
Source
-
Internal Group questions of Nanyang Institute of Technology's first monthly competition team
-
Uploaded
-
Zhang yuncong is a classic and disgusting question. He did not try out the error at last night, fortunately, we just found out the error. The key is to convert the infinite loop decimals into the simplest scores: For x =. in BC, a is the integer part, B is the fractional non-repeating part, and C is the fractional repeating part. The method for converting to the fractional form is: X multiplied by 10 (strlen (B) + strlen (c) to the power-x multiplied by 10 strlen (B) to the power/(10 (strlen (B) + strlen (c) to the power of-10 strlen (B) to the power), and other forms of decimal conversion are similar.
#include <stdio.h>#include <string.h>#define maxn 22typedef long long LL;char S[maxn], T[maxn], R[maxn];int t;LL a[2], b[2], c[3];void getData(LL arr[]) {scanf("%s%s%s", S, T, R);if(S[0] == '$') S[0] = '0';sscanf(S, "%lld", &c[0]);if(R[0] == '$') {if(T[0] == '$') {arr[0] = c[0]; arr[1] = 1;} else {strcat(S, T);sscanf(S, "%lld", &arr[0]);LL tmp = 1;int len = strlen(T);for(int i = 0; i < len; ++i)tmp *= 10;arr[1] = tmp;}} else {if(T[0] == '$') {strcat(S, R);sscanf(S, "%lld", &arr[0]);int len = strlen(R);LL tmp = 1;for(int i = 0; i < len; ++i)tmp *= 10;arr[0] -= c[0];arr[1] = tmp - 1;} else {strcat(S, T);sscanf(S, "%lld", &c[1]);strcat(S, R);sscanf(S, "%lld", &c[2]);int len = strlen(T);LL tmp = 1;for(int i = 0; i < len; ++i)tmp *= 10;LL tmp2 = tmp; len = strlen(R);for(int i = 0; i < len; ++i)tmp2 *= 10;arr[0] = c[2] - c[1];arr[1] = tmp2 - tmp;}}}LL gcd(LL x, LL y) {return y ? gcd(y, x % y) : x;}void solve() {c[0] = a[0] * b[1] + a[1] * b[0];c[1] = a[1] * b[1];if(c[0] % c[1] == 0) {printf("%lld\n", c[0] / c[1]);return;}LL tmp = gcd(c[0], c[1]);c[0] /= tmp; c[1] /= tmp;printf("%lld/%lld\n", c[0], c[1]);}int main() {// freopen("stdin.txt", "r", stdin);scanf("%d", &t);while(t--) {getData(a);getData(b);solve();}return 0;}
Nyoj131 decimal addition [String]