Floating Point addition 9-degree oj

Source: Internet
Author: User

Question [html] view plaincopy Description: Calculate the sum of two floating point numbers and the floating point numbers displayed in the input and output of the question are in the following format: P1P2... pi. q1Q2... qj for integer, P1P2... pi is a non-negative integer for the fractional part, and Qj is not equal to 0 input: for each group of cases, 1st rows are the number of test data groups n, each group of test data occupies 2 rows, they are two integers. There is an empty row between each group of test data, and each row of data cannot exceed 100 characters. In each group of cases, there are n rows, and each group of test data has a row of output corresponding to and. The output must be a floating-point number sample with a decimal number not 0. Input: 2 0.111111111111111111111111111111 0.111111111111111111111111111111 10000000.655555555555555555555555555555 1.444444444444444444444444444445 0.222222222222222222222222222222 sample output: 10000002.1 train of thought. This question should be a big integer addition, but pay attention to too many details. I suggest a few points: Be careful, break down the integer part and the fractional part, and add them separately. Pay attention to the carry of the large integer addition. Pay attention to a test case, 0.0 + 0.0 should = 0.0 pay attention to the truncation of the integer part starting 0, and the truncation of the last 0 in the fractional part AC code [cpp] # include <stdio. h> # include <string. h> # include <stdlib. h> # Define LEN 101 int ia [LEN], fa [LEN], ib [LEN], fb [LEN], ic [LEN], fc [LEN]; int main () {char str1 [LEN], str2 [LEN]; int I, j, k, n, l1, l2, lai, laf, lbi, lbf, temp, flmax, ilmax; while (scanf ("% d", & n )! = EOF) {while (n --) {// initialize memset (ia, 0, sizeof (ia); memset (fa, 0, sizeof (fa )); memset (ib, 0, sizeof (ib); memset (fb, 0, sizeof (fb); memset (ic, 0, sizeof (ic); memset (fc, 0, sizeof (fc); // receives the first floating point scanf ("% s", str1); l1 = strlen (str1 ); // construct an integer array for (I = 0; I <l1 & str1 [I]! = '. '; I ++) {ia [I] = str1 [I]-'0';} lai = I; // replace digits for (j = 0, k = lai-1; j <= lai/2 & j <k; j ++, k --) {temp = ia [j]; ia [j] = ia [k]; ia [k] = temp;} // construct the fractional array for (I + = 1; I <l1; I ++) {fa [I-1-lai] = str1 [I]-'0';} laf = I-1-lai; // receives the second floating point scanf ("% s", str2); l2 = strlen (str2); // constructs an integer array for (I = 0; I <l2 & str2 [I]! = '. '; I ++) {ib [I] = str2 [I]-'0';} lbi = I; // replace digits for (j = 0, k = lbi-1; j <= lbi/2 & j <k; j ++, k --) {temp = ib [j]; ib [j] = ib [k]; ib [k] = temp;} // construct the fractional array for (I + = 1; I <l2; I ++) {fb [I-1-lbi] = str2 [I]-'0';} lbf = I-1-lbi; // whose decimal places are more flmax = (laf> = lbf )? Laf: lbf; int c = 0; // fractional carry for (I = 0, j = flmax-1; j> = 0; j --, I ++) {fc [I] = fa [j] + fb [j] + c; if (fc [I]> = 10) {c = fc [I]/10; fc [I] % = 10;} else {c = 0 ;}// integer addition ilmax = (lai> = lbi )? Lai: lbi; for (I = 0; I <ilmax; I ++) {ic [I] = ia [I] + ib [I] + c; if (ic [I] >=10) {c = ic [I]/10; ic [I] % = 10;} else {c = 0 ;}} while (c) {ic [ilmax ++] = c % 10; c/= 10 ;} // print the final result // find the first integer not 0 for (j = ilmax-1; j> = 0; j --) {if (ic [j]! = 0) {break ;}}if (j> = 0) {for (I = j; I> = 0; I --) {printf ("% d ", ic [I]) ;}} else {printf ("0");} printf (". "); // find the last decimal place not 0 for (j = 0; j <flmax-1; j ++) {if (fc [j]! = 0) {break ;}}for (I = flmax-1; I >= j; I --) {printf ("% d", fc [I]);} printf ("\ n"); // receives the empty row getchar () ;}} return 0 ;} /*************************************** * *********************** Problem: 1137 User: wangzhengyi Language: C Result: Accepted Time: 290 MS Memory: 912 kb ************************************** **************************/

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.