Zookeeper
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1753
Daming A + B
Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 8296 accepted submission (s): 2929
According to Problem description, after more than a month, James has grown a lot, so he changed his name to "Daming ".
At this time, he is no longer the "James" who will only add less than 100, and now he will even add positive decimal places of any length.
Now, let's give you two positive decimal places a and B. Your task is to calculate the value of A + B on behalf of Daming.
The input question contains multiple groups of test data. Please process the data until the end of the file.
Each group of test data contains two decimal places a and B whose length is not greater than 400.
Output: output the value of A + B in one row. Please output the simplest form. For detailed requirements, see sample output.
Sample Input
1.1 2.91.1111111111 2.34443233431 1.1
Sample output
43.45554344542.1
The idea is clear, and the algorithm only uses a large number, but there are many points to note.
Calculate the sum of decimal places first, because it is very likely that digits are carried to everyone. When the carry value is a [0] + = 1, the rows can be used directly later;
Then calculate the sum of the integer, that is, the sum of the large numbers, but note that when the number in the string is saved to, to write a [J] + = S1 [I]-'0', because carry should be considered
The code is followed by several groups of data. Once this is done, you can access
/* The idea is clear, and the algorithm only uses a large number, but note that many places in it calculate the sum of decimal places first, because it is very likely to be carried to you, when the carry value is a [0] + = 1, it will be used directly behind it. Then, the sum of the integer, that is, the sum of the large numbers, is calculated, when saving the number in the string to a [], write it as a [J] + = S1 [I]-'0 ', because carry */# include <stdio. h> # include <string. h> # define Max distinct int main () {int A [Max], B [Max], num_1 [Max], num_2 [Max], I, J, K, N; char S1 [Max], S2 [Max]; while (~ Scanf ("% S % s", S1, S2) {memset (num_1, 0, sizeof (num_1); memset (num_2, 0, sizeof (num_2 )); memset (A, 0, sizeof (a); memset (B, 0, sizeof (B); n = strlen (S1); for (I = 0; I <n; I ++) // S1 [I] is the decimal point if (S1 [I] = '. ') break; For (k = 0, j = n-1; j> I; j --, K ++) // S1 has K decimal places num_1 [k] = S1 [J]-'0'; int I _1 = I, k_1 = K, n_1 = N; N = strlen (S2); for (I = 0; I <n; I ++) // S2 [I] is the decimal point if (s2 [I] = '. ') break; For (k = 0, j = n-1; j> I; j --, K ++) // S2 has K decimal places num_2 [k] = S2 [J]-'0'; int I _2 = I, K_2 = K, N_2 = N; If (k_1> = K_2) // Add decimal places. Pay attention to the very bit carry-on situation. // divide the two places (k_1> = K_2) and (k_1 <K_2) is not sure which decimal point after the large digits {// If K_1> = K_2, put the calculation result in num_1 [] for (I = k_1-k_2, j = 0; I <K_1; I ++, J ++) {num_1 [I] + = num_2 [J]; If (num_1 [I]> = 10) {num_1 [I]-= 10; num_1 [I + 1] + = 1;} // printf ("% d # \ n", num_1 [I]);} If (num_1 [I] = 1) // The very bit enters the single position {num_1 [I]-= 1; A [0] + = 1 ;}} else if (K_2> K_1) // If K_2> K_1, put the calculated result in num_2. {For (I = k_2-k_1, j = 0; I <K_2; I ++, J ++) {num_2 [I] + = num_1 [J]; if (num_2 [I]> = 10) {num_2 [I]-= 10; num_2 [I + 1] + = 1 ;}} if (num_2 [I] = 1) // The very bit enters the single position {num_2 [I]-= 1; A [0] + = 1 ;}} // The top decimal point plus two cases, the lower decimal point output is also divided into two cases for (j = 0, I = I _1-1; I> = 0; I --, j ++) // here is a [J] + = S1 [I]-'0', because a [0] carries a decimal number. A [J] + = S1 [I]-'0'; // only a [0] + = S1 [O]-'0 ', the rest is still a [J] = S1 [I]-'0', but the write effect is the same as separating a [0, write for (j = 0, I = I _2-1; I> = 0; I --, J ++) // The value assigned to B [0] is the same as that assigned to a [] B [J] = S2 [I]-'0'; for (I = 0; I <Max; I ++) // calculate the integer part and store it in a [] {A [I] + = B [I]; if (a [I]> = 10) {A [I]-= 10; A [I + 1] + = 1 ;}}for (I = MAX-1; I> 0; I --) // output the integer part if (a [I]! = 0) break; For (; I> = 0; I --) printf ("% d", a [I]); If (k_1> = K_2) // output decimal part {for (I = k_1-1; I> = 0; I --) // The for loop here is to judge whether there is a value after the decimal point, if there is no value, loop to the end, and finally I is-1 If (num_1 [I]! = 0) break; // If I =-1, the following if (I! =-1), that is, 0 for (j = 0; j <K_1; j ++) after the decimal point is not output) // This for loop does not output 0 after the calculated decimal point, for example, 1.028 + 1.072. The result is 2.100 before the for statement is executed. After the for statement is executed, the following 0 is removed and the result is 2.1 If (num_1 [J]! = 0) break; if (I! =-1) // if there is a value after the decimal point, output the number of the decimal point and the number after the {printf (". "); for (I = k_1-1; I> = J; I --) printf (" % d ", num_1 [I]) ;}} else if (k_1 <K_2) // here the IF is the same as the above {for (I = k_2-1; I> = 0; I --) if (num_2 [I]! = 0) break; For (j = 0; j <K_2; j ++) if (num_2 [J]! = 0) break; if (I! =-1) {printf (". "); for (I = k_2-1; I> = J; I --) printf (" % d ", num_2 [I]);} printf ("\ n");} return 0;}/* 0 01.2333 1.266799999 255.1.20.299999.889 0.1111000.0 255.255.112233.1 112121213213124342545354545 333.9123450000 777123400000 777.7000.000 0.20.1212121212.1111111 21212121.33300000000000000 */