long_long_2014 @ 2024-11-19 17:25:56
#include<bits/stdc++.h>
using namespace std;
string js1,js2;
int a[2010],b[2010],c[2010];
int i,lena,lenb,lenc,lx,ly,j;
int main()
{
cin>>js1>>js2;
lx=js1.size();
ly=js2.size();
lena=lx;
lenb=ly;
for(i=0;i<=lena-1;i++)
{
a[i]=js1[lena-i-1]-'0';
}
for(i=0;i<=lenb-1;i++)
{
b[i]=js2[lenb-i-1]-'0';
}
for(i=0;i<=lena-1;i++)
{
for(j=0;j<=lenb-1;j++)
{
c[i+j]=a[i]*b[j];
}
}
lenc=lena+lenb;
for(i=0;i<=lenc-1;i++)
{
if(c[i]>=10)
{
c[i+1]+=c[i]/10;
c[i]%=10;
}
}
while(c[lenc-1]==0&&lenc>1)
{
lenc--;
}
for(i=lenc-1;i>=0;i--)
{
cout<<c[i];
}
return 0;
}
by _SB_ @ 2024-12-02 21:16:52
c数组开大点
至少是a数组个数+b数组个数
实在不行的话用这个
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string s1 = "";
string s2 = "";
int a[2100] = {};
int b[2100] = {};
int c[4200] = {};
cin >> s1 >> s2;
for (int i=0; i<s1.size(); i++)
{
a[i] = s1[s1.size()-1-i] - '0';
}
for (int i=0; i<s2.size(); i++)
{
b[i] = s2[s2.size()-1-i] - '0';
}
for (int i=0; i<s2.size(); i++)
{
for (int j=0; j<s1.size(); j++)
{
c[j+i] = c[j+i] + a[j]*b[i];
if (c[j+i] >= 10)
{
c[j+i+1] += c[j+i] / 10;
c[j+i] = c[j+i] % 10;
}
}
}
int index = 0;
int len = s1.size()+s2.size();
for (int i=len-1; i>=0; i--)
{
if (c[i] != 0)
{
index = i;
break;
}
}
for (int i=index; i>=0; i--)
{
cout << c[i];
}
return 0;
}
by long_long_2014 @ 2024-12-20 13:01:52
谢谢,已过,已关