//这里我先将两个字符串小数点前后补齐到相同的长度,再一一对位相加 (这里记得进位 #include <iostream> #include<string> using namespace std; int main() { string a,b; string longer,shorter; while (getline(cin,a)) { // 注意 while 处理多个 case getline(cin,b); int find1=a.find('.'); int find2 = b.find('.');//小数点位置 if(find1<find2){ longer = b; shorter =a; } else{ longer =a; shorter = b; }//为了方便最高一位进位,在最高位前添加一位 longer.insert(0,"0"); do{ //补全小数点前的0 shorter.insert(0,"0"); find1=longer.find('.'); find2 = shorter.find('.'); } while(find1!=find2); if(longer.size()<shorter.size()){ swap(longer,shorter); } while(longer.size()!=shorter.size()){ shorter.insert(shorter.size(),"0"); //补全小数点后的0 } int flag=0; for(int i=longer.size()-1;i>=0;i--){ if(longer[i]=='.') continue; int res =(longer[i]-'0')+(shorter[i]-'0')+flag; flag=res/10; res%=10; longer[i]=res+'0'; } if(flag!=0){ longer[0]=flag+'0'; cout<<longer; } else{//没有进位的话直接输出 cout<<longer.substr(1,longer.size()); } } }