只有20分,麻烦大佬详细讲一下思路

P1303 A*B Problem

S1746 @ 2022-11-18 20:27:00


#include<iostream>
using namespace std;
void print(int *k,int len)
{
  for(int i=len; i>=0; i--)
  {
    cout<<*(k+i);
  }
  //cout<<endl;
}//输出以len~0顺序
int main()
{
  string a,b;
  int c[4050];
  cin>>a>>b;
  if(a[0]=='0'||b[0]=='0')
  {
    cout<<0;
    return 0;
  }
  int len=a.length()+b.length();
  for(int i=0; i<int(a.length()); i++)
  {
    a[i]-='0';
  }
  for(int i=0; i<int(b.length()); i++)
  {
    b[i]-='0';
  }
  for(int i=0; i<a.length(); i++)
  {
    for(int ii=0; ii<b.length(); ii++)
    {
      int k=a.length()-i-1+b.length()-ii-1;
      c[k]+=a[a.length()-i-1]*b[b.length()-ii-1];//c[0]为末尾
    }
  }
  //int mm=0;
  len--;
  //print(c,len);
  while(c[len]==0)
  {
    len--;
  }
  for(int i=0; i<=len; i++)//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
  {
    c[i+1]+=c[i]/10;
    c[i]=c[i]%10;
  }
  if(c[len+1])
  {
    len++;
  }
  print(c,len);
  return 0;
}
/*375
   *34
   ___
  1500
 1125
 _____
 12750
 从0~len进位*/

by ysdmz @ 2022-12-05 18:47:45

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3000000;

struct num{
    int a[N],len;
};

void read(num &x){
    string s;
    cin>>s;
    x.len=s.size();
    memset(x.a,0,sizeof(x.a));
    for(int i=0;i<s.size();i++){
        x.a[x.len-i]=s[i]-'0';
    }
}

void print(num x){
    for(int i=x.len;i;i--){
        putchar(x.a[i]+'0');
    }
}

num operator * (num x,num y){
    num ret;
    ret.len=x.len+y.len;
    memset(ret.a,0,sizeof(ret.a));
    for(int i=1;i<=x.len;i++){
        for(int j=1;j<=y.len;j++){
            ret.a[i+j-1]+=x.a[i]*y.a[j];
        }
    }
    for(int i=1;i<=ret.len;i++){
        ret.a[i+1]+=ret.a[i]/10;
        ret.a[i]%=10;
    }
    while(ret.len&&ret.a[ret.len]==0){
        ret.len--;
    }
    if(!ret.len){
        ret.len++;
    }
    return ret;
}

num x,y,ans;

int main(){
    read(x);
    read(y);
    ans=x*y;
    print(ans);
}

|