Hdu1753 (simulate adding large real numbers) and hdu1753 real numbers
Question information: manually calculate the addition of large real numbers
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1753
AC code:
/**
* Add large real numbers, which are separated by decimal points, simulate addition operations, and merge the values.
*/
# Include <iostream>
# Include <string>
# Include <algorithm>
Using namespace std;
String add (string s1, string s2) {// The string simulates the large integer addition, and the simulation result has a leading 0
Int I, j, len1, len2;
Len1 = s1.size (); len2 = s2.size ();
// Find the decimal point
For (I = 0; s1 [I]! = '.' & I <len1; I ++ );
For (j = 0; s2 [j]! = '.' & J <len2; j ++ );
// Cout <I <"" <j <endl;
String s, ss;
Int flag = 0;
// Simulate the sum of digits after the decimal point
Len1 --; len2 --;
While (len1-i> len2-j ){
Int sum = flag + (s1 [len1 --]-'0 ');
S + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
// Cout <s <endl;
While (len2-j> len1-i ){
Int sum = flag + (s2 [len2 --]-'0 ');
S + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
// Align the digits after the decimal point and add them together
Int len11, len22;
If (len1-i> len2-j ){
Len11 = len1-(len1-i)-(len2-j ));
Len22 = len2;
}
Else {
Len22 = len2-(len2-j)-(len1-i ));
Len11 = len1;
}
While (len11> I & len22> j ){
Int sum = flag + (s1 [len11 --]-'0') + (s2 [len22 --]-'0 ');
S + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
/** The iterator removes the character '0'
While (len11> I ){
Int sum = flag + (s1 [len11 --]-'0 ');
S + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
While (len22> j ){
Int sum = flag + (s2 [len22 --]-'0 ');
S + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
String: iterator it; // defines the direction iterator.
For (it = s. begin (); * it = '0' & it! = S. end (); it ++) {// remove the prefix 0 of s, that is, the decimal 0
S. erase (it );
}**/
// Cout <s <endl;
String ss0; // use ss0 to remove leading 0
Int kk;
For (kk = 0; kk <s. size () & (s [kk] = '0' | s [kk] = '. '); kk ++ );
For (int k = kk; k <s. size (); k ++) ss0 + = s [k];
// Cout <ss0 <endl;
For (int I = 0; I <ss0.size ()/2; I ++ ){
Char c = ss0 [I];
Ss0 [I] = ss0 [ss0.size ()-i-1];
Ss0 [ss0.size ()-i-1] = c;
}
// Simulate adding the number before the decimal point
Len1 = I-1; len2 = J-1;
While (len1>-1 & len2>-1 ){
Int sum = flag + (s1 [len1 --]-'0') + (s2 [len2 --]-'0 ');
Ss + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
While (len1>-1 ){
Int sum = flag + (s1 [len1 --]-'0 ');
Ss + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
While (len2>-1 ){
Int sum = flag + (s2 [len2 --]-'0 ');
Ss + = char (sum) % 10 + '0 ');
Flag = sum/10;
}
If (flag) ss ++ = char ('0' + flag );
// Cout <s <endl;
For (int I = 0; I <ss. size ()/2; I ++ ){
Char c = ss [I];
Ss [I] = ss [ss. size ()-i-1];
Ss [ss. size ()-i-1] = c;
}
// Cout <ss <endl;
If (! Ss0.empty () & ss0 [0]! = '.') Ss = ss + '.' + ss0;
If (! Ss0.empty () & ss0 [0] = '.') ss = ss + ss0;
Return ss;
}
Int main ()
{
String s1, s2;
While (cin> s1> s2 ){
Cout <add (s1, s2) <endl;
}
Return 0;
}
Why avoid directly adding or subtracting a large real number from a small real number?
People generally record a maximum of 5-7 digits, and the numbers behind them are rounded down. "A large real number and a small real number are directly added or subtracted, A very small real number is attached to a very large real number and is eligible to be rounded off. Therefore, avoid directly adding or subtracting a large real number and a small real number. In this case, large numbers are swallowed to decimal places, causing errors because the computer's data storage memory (number of digits) is limited.
For example, the calculation result of 100-0.0000000005 is 100.
Also, we need to avoid decimal points as denominator.
Write a program and complete the following: 1, prompting the user to input any three real numbers 2, showing three real numbers 3, adding these three real numbers,
# Include <stdio. h>
Void main ()
{
Int I = 0, j = 0, k = 0;
Printf ("Enter three real numbers: \ n ");
Scanf ("% d", & I, & j, & k );
Printf ("the input three real numbers are \ n ");
Printf ("% 6d % 6d % 6d \ n", I, j, k );
Printf ("the sum of the three real numbers: \ n ");
Printf ("% 6d \ n", I + j + k );
}