Home Article Practice 高精度乘法和加法函数(c++)(自编)

高精度乘法和加法函数(c++)(自编)

2024-08-01 11:20  views:214  source:4664946    

#include <bits/stdc++.h>
using namespace std;
string gjdjf(string s,string c){
//cout<<c<<" "<<s<<' ';
int len=s.size();
int len2=c.size();
int llen=max(len,len2);
if(len<llen){
string t="";
for(int i=0;i<llen-len;i++){
t+="0";
}
t+=s;
s=t;
}
if(len2<llen){
string t="";
for(int i=0;i<llen-len2;i++){
t+="0";
}
t+=c;
c=t;
}
string ansstr="";
int jw=0;
for(int i=llen-1;i>=0;i--){
int jl=(s[i]-'0')+(c[i]-'0')+jw;
ansstr=to_string(jl%10)+ansstr;
jw=jl/10;
}
if(jw!=0){
ansstr=to_string(jw)+ansstr;
}
//cout<<ansstr<<' '<<c<<" "<<s<<endl;
return ansstr ;
}
string gjdcf(string s,string c){
int len=s.size();
int len2=c.size();
int llen=max(len,len2);
string ansstr="";
for(int i=0;i<len;i++){
int jw=0;
string w="";
for(int j=len2-1;j>=0;j--){
w=to_string(((s[i]-'0')*(c[j]-'0')+jw)%10)+w;
jw=((s[i]-'0')*(c[j]-'0')+jw)/10;
//cout<<"s[i]: "<<s[i]<<"c[j]: "<<c[j]<<endl;
}
if(jw!=0){
w=to_string(jw)+w;
}
string t=ansstr+"0";
ansstr=gjdjf(w,t);
}
return ansstr;
}
int main(){
#计算m+n
string n,m;
cin>>n>>m;
cout<<gjdjf(n,m);
#计算m*n
string n,m;
cin>>n>>m;
cout<<gjdcf(n,m);
return 0;
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)