Jerry555 @ 2024-02-17 17:22:55
#include <iostream>
#include <string>
using namespace std;
class Basic_tools {
public:
std::string removeLeadingZeros(const std::string& str) {
size_t pos = str.find_first_not_of('0');
if (pos != std::string::npos) {
return str.substr(pos);
}
return "0";
}
}JTools;
class BigNumber {
public:
std::string number;
int len;
bool empty = true;
BigNumber() {
number="";
len=0;
}
BigNumber(std::string NewNum) {
number = NewNum;
len = NewNum.size();
empty = false;
}
void InitIn() {
std::cin >> number;
len = number.size();
empty = false;
}
void Output() {
std::cout << number;
}
void Delete() {
empty = true;
number = "";
len = 0;
}
};
BigNumber operator*(BigNumber a, BigNumber b) {
int A[5010]={0}, B[5010]={0},D[5010]={0};
//BigNumber D;
for (int i = a.len-1; i >= 0; i--)A[a.len - i] = a.number[i] - '0';
for (int i = b.len-1; i >= 0; i--)B[b.len - i] = b.number[i] - '0';
for (int i = 1; i <= a.len; i++)for (int j = 1; j <= b.len; j++) D[i + j - 1] += A[i] * B[j];
int len=a.len+b.len;
for (int i = 1; i <= len; i++) {
D[i + 1] += D[i] / 10;
D[i] %= 10;
}
string Ds;
for(int i=max(1,len);i>=1;i--){
Ds.insert(0,to_string(D[i]+'0'));
}
JTools.removeLeadingZeros(Ds);
return BigNumber(Ds);
}
int main(){
BigNumber AB,BB;
AB.InitIn();
BB.InitIn();
BigNumber CB;
CB = AB * BB;
CB.Output();
}
by wuzhiyuan123 @ 2024-02-21 20:58:08
dalao啊,你这个代码我看着都不像这题的,我都看不懂了,有点复杂,我给你我的代码参考一下
by wuzhiyuan123 @ 2024-02-21 21:07:41
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
string a1,b1;
int a[1000010],b[1000010],c[1000010];
int main()
{
//freopen("input.txt", "r", stdin);//这行代码在提交的时候记得删掉,如果你不知道这行代码的意思可以删掉
cin >> a1 >> b1;
int len1 =0;
//不想算了,特判一下
if((a1.length()==1&&a1[1]==0)||(b1.length()==1&&b1[1]==0)){
cout << 0;
return 0;
}
//输入,吧正着的倒过来
for(int i=a1.length()-1, j=1;i>=0;i--, j++){
a[j] = a1[i] - '0';
}
for(int i=b1.length()-1, j=1;i>=0;i--, j++){
b[j] = b1[i] - '0';
}
//开始做乘法,把a[i]*b[j]的结果放的c[i+j-1],至于为什么这么放画个表格就知道了
for(int i=1;i<=a1.length();i++){
for(int j=1;j<=b1.length();j++){
c[i+j-1]+=a[i]*b[j];
len1=max(i+j-1, len1);
}
}
//进位
int len2=len1;
for(int i=1;i<=len2;i++){
if(c[i]>=10){
int change = c[i]/10;
c[i]=c[i]%10;
c[i+1]+=change;
if(i==len2) len1++;
}
}
//倒着输出得到正的数
for(int i=len1;i>0;i--){
cout << c[i];
}
return 0;
}