大数相乘
出错信息如下:
Expression:string subscript out of range
Code:
#include<iostream>
#include<string>
using namespace std;
string s;
int n;
string chengji(s.length()*n,'0');
string muling()
{
int i,j,k;
for(i=s.length()-1;i>=0;--i)
if(s[i])
{
for(j=s.length()-1,k=i+s.length();j>=0;--j,--k)
{
int temp=(s[i]-'0')*(s[j]-'0');
chengji[k-1]=char(chengji[k-1]-'0'+(chengji[k]-'0'+temp)/10);
chengji[k]=char(chengji[k]-'0'+temp%10);
}
}
return chengji.substr(chengji.find_first_not_of('0'));
}
int main()
{
//freopen("input.txt","r",stdin);
while(cin>>s>>n)
{
string result;
int i;
for(i=0;i<n;++i)
result=muling();
cout<<result<<endl;
}
return 0;
}
本人刚接触C++,请教高手,求解答,谢谢