//描述 //求2个浮点数相加的和 题目中输入输出中出现浮点数都有如下的形式: //P1P2...Pi.Q1Q2...Qj 对于整数部分,P1P2...Pi是一个非负整数 对于小数部分,Qj不等于0 //输入描述: //对于每组案例,每组测试数据占2行,分别是两个加数。 //输出描述: //每组案例是n行,每组测试数据有一行输出是相应的和。 输出保证一定是一个小数部分不为0的浮点数 #include <iostream> #include <cstdio> #include <cstring> using namespace std; string Add(string str1, string str2) { string fractional1 = str1.substr(str1.find('.') + 1); string fractional2 = str2.substr(str2.find('.') + 1); if (fractional1.size() < fractional2.size()) { //小数对齐 fractional1 = fractional1 + string(fractional2.size() - fractional1.size(), '0'); } else { fractional2 = fractional2 + string(fractional1.size() - fractional2.size(), '0'); } string integral1 = str1.substr(0, str1.find('.')); string integral2 = str2.substr(0, str2.find('.')); if (integral1.size() < integral2.size()) { //整数对齐 integral1 = string(integral2.size() - integral1.size(), '0') + integral1; } else { integral2 = string(integral1.size() - integral2.size(), '0') + integral2; } int carry = 0; string fractional(fractional1.size(), ' '); for (int i = fractional.size() - 1; i >= 0; --i) { //小数相加 int current = fractional1[i] - '0' + fractional2[i] - '0' + carry; fractional[i] = current % 10 + '0'; carry = current / 10; } string integral(integral1.size(), ' '); for (int i = integral.size() - 1; i >= 0; --i) { //整数相加 int current = integral1[i] - '0' + integral2[i] - '0' + carry; integral[i] = current % 10 + '0'; carry = current / 10; } if (carry != 0) { integral = to_string(carry) + integral; } return integral + '.' + fractional; } int main() { string str1, str2; while (cin >> str1 >> str2) { cout << Add(str1, str2) << endl; } return 0; }