先将整数部分和小数部分对齐,后面就跟高精度加法类似思路了,最后将末尾多余的零删除。
string sa,sb;
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>sa>>sb;
int posa=sa.find('.'),posb=sb.find('.');
int ra=sa.size()-posa,rb=sb.size()-posb;
if(posa > posb) sb=string(posa-posb,'0')+sb;
else sa=string(posb-posa,'0')+sa;
if(ra > rb) sb=sb+string(ra-rb,'0');
else sa=sa+string(rb-ra,'0');
reverse(sa.begin(),sa.end());
reverse(sb.begin(),sb.end());
int carry=0;
string res;
for(int i=0;i<sa.size();i++)
{
if(sa[i] == '.')
{
res+='.';
continue;
}
int t=(sa[i]-'0')+(sb[i]-'0')+carry;
res+='0'+t%10;
carry=t/10;
}
if(carry) res+='0'+carry;
reverse(res.begin(),res.end());
while(res[res.size()-1] == '0') res.erase(res.size()-1);
cout<<res<<endl;
}
//system("pause");
return 0;
}