高精度加法模板

来源:互联网 发布:外资企业数据 编辑:程序博客网 时间:2024/05/16 19:32

自己写的,效率很低,见笑见笑

string add(string str1,string str2){    int len1=str1.size();    int len2=str2.size();    if(len1<len2){        swap(str1,str2);        swap(len1,len2);    }    int c=0;    for(int i=len1-1,j=len2-1;i>=0;i--,j--){        int temp=0;        if(j>=0)            temp+=str2[j]-'0';        temp+=str1[i]+c-'0';        str1[i]=temp%10+'0';        c=temp/10;    }    if(c)        str1='1'+str1;    return str1;}


0 0
原创粉丝点击