String 字符串乘法:花费太多时间
我正在尝试制作一个有效的大数乘法程序,并提出了以下解决方案:-String 字符串乘法:花费太多时间,string,c++14,String,C++14,我正在尝试制作一个有效的大数乘法程序,并提出了以下解决方案:- #include <iostream> using namespace std; string Addem(string a,string b) // To add two large numbers,only for +ve numbers { string c; int n=a.size(),m=b.size(); if(m>n) { swap(n,m);
#include <iostream>
using namespace std;
string Addem(string a,string b) // To add two large numbers,only for +ve numbers
{
string c;
int n=a.size(),m=b.size();
if(m>n)
{
swap(n,m);
swap(a,b);
}
for(int i=0; i<n-m; i++) // adding zeros to make lengths of both string equal.
b='0'+b;
int carry=0,curr=0;
for(int i=n-1; i>-1; i--)
{
curr=a[i]-'0'+b[i]-'0'+carry; //basic school math to find sum, using
carry=curr/10; //two variables to find sum of each place individually
curr=curr%10; //and passing carry to the next position.
c=(char)(curr+'0')+c;
if(i==0 && carry)
{
c=(char)(carry+'0')+c;
}
}
while(c[0]=='0' && c.size()!=1) //removing any leading zeros
c.erase(0,1);
return c;
}
string Multiplyem(string a,string b) // To multiply two large numbers.
{
bool sign=1;
if( (a[0]=='-' && b[0]=='-') || (a[0]!='-' && b[0]!='-') )
sign=0;
if(a[0]=='-')
a.erase(0,1); //taking care of sign.
if(b[0]=='-')
b.erase(0,1);
if(a=="0" || b=="0")
return "0";
string c;
int curr=0,carry=0;
int n=a.size(),m=b.size();
for(int i=m-1; i>-1; i--)
{
string tmp; //string to store result of a*(current digit of b)
for(int j=n-1; j>-1; j--)
{
curr=carry+(b[i]-'0')*(a[j]-'0'); //scroll down for this,explained
carry=curr/10; //the logic for this under EDIT
curr=curr%10;
tmp=(char)(curr+'0')+tmp;
if(j==0 && carry)
{
tmp=(char)(carry+'0')+tmp;
}
}
for(int j=m-1; j>i; j--) //adding zeros take care of number positions
tmp+='0';
c=Addem(tmp,c); //adding tmp to c
carry=0,curr=0;
}
while(c[0]=='0' && c.size()!=1) // removing any leading zeros (if any)
c.erase(0,1);
if(sign==1 && c!="0") //adding sign (if reqired)
c='-'+c;
return c;
}
int main()
{
string a,b;
cin>>a>>b;
cout<<"Multiplication = "<<Multiplyem(a,b)<<" \n \n";
}
#包括
使用名称空间std;
string Addem(string a,string b)//添加两个大数字,仅用于+ve数字
{
字符串c;
int n=a.size(),m=b.size();
如果(m>n)
{
交换(n,m);
掉期(a、b);
}
对于(int i=0;i-1;i--)
{
curr=a[i]-'0'+b[i]-'0'+进位;//求和的基础学校数学,使用
carry=curr/10;//两个变量分别查找每个位置的和
curr=curr%10;//并将进位传递到下一个位置。
c=(字符)(curr+'0')+c;
if(i==0&&carry)
{
c=(字符)(进位+0')+c;
}
}
而(c[0]='0'&&c.size()!=1)//删除任何前导零
c、 擦除(0,1);
返回c;
}
string Multiplyem(string a,string b)//将两个大数字相乘。
{
布尔符号=1;
如果((a[0]='-'&&b[0]='-')| |(a[0]!='-'&&b[0]!='-'))
符号=0;
如果(a[0]='-')
a、 擦除(0,1);//注意符号。
如果(b[0]='-')
b、 擦除(0,1);
如果(a==“0”| | b==“0”)
返回“0”;
字符串c;
int curr=0,进位=0;
int n=a.size(),m=b.size();
对于(int i=m-1;i>-1;i--)
{
string tmp;//存储a*结果的字符串(b的当前数字)
对于(int j=n-1;j>-1;j--)
{
curr=carry+(b[i]-'0')*(a[j]-'0');//向下滚动查看,解释如下
carry=curr/10;//此项的逻辑正在编辑中
当前=当前%10;
tmp=(字符)(curr+'0')+tmp;
如果(j==0&进位)
{
tmp=(字符)(进位+0')+tmp;
}
}
对于(int j=m-1;j>i;j--)//加零处理数字位置
tmp+='0';
c=Addem(tmp,c);//将tmp添加到c
进位=0,当前=0;
}
while(c[0]='0'&&c.size()!=1)//删除任何前导零(如果有)
c、 擦除(0,1);
如果(符号==1&&c!=“0”)//添加符号(如果需要)
c='-'+c;
返回c;
}
int main()
{
a、b串;
cin>>a>>b;
cout在您的解决方案中,您正在计算每个数字乘法的临时字符串,并将其增量相加到最终字符串。
跳过它,切换到一个通用的可变结构(比如CFG解决方案中的结果向量),只在正确的位置更新该结构
随着人数的增长,临时工将是
e.g. 100000*29990
0
90
900
9000
20000
对于每个temp+c操作,最终都会有很多零。对于所有这些操作,您仍然在执行类似于0+0或0+(非零数字)的操作这些都是不必要的。如果您能解释一下您提出的算法,我们将不胜感激。请尝试以步骤的形式给出,然后描述您是如何实现每个步骤的。这样,我们就可以更快地了解您的算法失败的原因。请为我的逻辑添加一些注释和解释。