Add high-precision decimal places

Source: Internet
Author: User

 

Question link:Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1753

Calculate the sum of two long decimal places

 

# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # define EPS 1e-6 # define Inf (1 <20) # define PI ACOs (-1.0) using namespace STD; char a [600], B [600]; int Lena, lenb, INTA [600], intb [600], ans [600]; int main () {While (scanf ("% S % s", a, B )! = EOF) {memset (INTA, 0, sizeof (INTA); memset (intb, 0, sizeof (intb); memset (ANS, 0, sizeof (ANS )); lena = strlen (a); lenb = strlen (B); int I = lena-1; while (A [I]! = '. ') I --; int Tempa; if (I <0) {Tempa = 0; Lena ++; // if it is an integer, unified} else Tempa = lena-1-i; int J = lenb-1; while (B [J]! = '. ') J --; int tempb; If (j <0) {tempb = 0; lenb ++;} else tempb = lenb-1-j; int later = max (Tempa, tempb ); // The maximum number of digits after the decimal point (I = later, j = Lena-Tempa; I >=1 & J <Lena; I --) {INTA [I] = A [J]-'0'; j ++;} // for (I = 1, j = later + 1; I <= lena-1-tempa; I ++, J ++) INTA [J] = A [lena-1-tempa-i]-'0'; // int temp = J-1 before the decimal point; for (I = later, j = lenb-tempb; I >=1 & J <lenb; I --) {intb [I] = B [J]-'0 '; j ++;} for (I = 1, j = later + 1; I <= lenb-1-tempb; I ++, J ++) intb [J] = B [lenb-tempb-1-i]-'0'; If (J-1> temp) temp = J-1; for (I = 1; I <= temp; I ++) {ans [I] + = INTA [I] + intb [I]; If (ANS [I]> = 10) ans [I + 1] = ans [I]/10; ans [I] = ans [I] % 10 ;} while (ANS [I] = 0 & I> later + 1) // remove the front zero. Note that only the Zero I before the decimal point can be removed --; if (I <= 0) {printf ("0 \ n"); Continue ;}for (j = 1; ans [J] = 0 & J <= later; j ++); // remove the trailing zero. Note that it is the zero for (; I> = J; I --) {if (I = later) putchar ('. '); printf ("% d", ANS [I]);} putchar (' \ n');} return 0;}/* 0.001 0.00999999 11.0 912 */

 

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.