80分求助

P1303 A*B Problem

12345limengqi @ 2023-11-04 19:27:13

#include<bits/stdc++.h>
using namespace std;
char s1[1010],s2[1010];
int a[2020],b[2020],c[2020];
void change(char *s,int a[110])
{
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        a[len-i-1]=s[i]-'0';
    }
}
void print(int c[110])
{
    int len=2010-1;
    while(c[len]==0&&len>0)
    {
        len--;
    }
    for(int i=len;i>=0;i--)
    {
        printf("%d",c[i]);
    }
}
int main()
{
    scanf("%s%s",s1,s2);
    change(s1,a);
    change(s2,b);
    int len1=strlen(s1);
    int len2=strlen(s2);
    for(int i=0;i<len1;i++)
    {
        for(int j=0;j<len2;j++)
        {
            c[i+j]+=a[i]*b[j];
        }
    }
    for(int i=0;i<2010;i++)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    print(c);
    return 0;
}

by 2021zjhs005 @ 2023-11-04 19:32:40

@12345limengqi

可能有误,不要责怪本蒟蒻。

by 12345limengqi @ 2023-11-04 19:47:11

@2021zjhs005 最后一个点从RE变成WA了


by heyx0201 @ 2023-11-04 19:57:37

你可以把它记录到一个类里面,然后直接调用。比如这样:

#include <bits/stdc++.h>

namespace Integer {
const std::string symbol = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <size_t kMaxLength, int binary = 10, typename T = int> class BigInt {
private:
  T n, a[kMaxLength * 2];
  bool f;

public:
  BigInt() { Init(); }
  T &operator[](int i) { return a[i]; }
  void Init() {
    n = f = 1;
    std::fill(a, a + kMaxLength, 0);
  }
  friend std::istream &operator>>(std::istream &tempStream, BigInt &a) {
    std::string s;
    tempStream >> s;
    a.n = s.size();
    if (s[0] == '-') {
      a.f = 0;
      a.n--, s.erase(0, 1);
    }
    for (int i = 0; i < a.n; i++) {
      a[i] = (s[a.n - i - 1] >= 'A' && s[a.n - i - 1] <= 'Z'
                  ? s[a.n - i - 1] - 'A' + 10
                  : s[a.n - i - 1] - '0');
    }
    return tempStream;
  }
  friend std::ostream &operator<<(std::ostream &tempStream, BigInt a) {
    for (; a.n > 1 && !a[a.n - 1]; a.n--) {
    }
    if (!a.f) {
      tempStream << '-';
    }
    for (int i = a.n - 1; i >= 0; i--) {
      tempStream << symbol[a[i]];
    }
    return tempStream;
  }
  BigInt operator*(BigInt x) {
    BigInt y;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < x.n; j++) {
        y[i + j] += a[i] * x[j];
      }
    }
    y.n = n + x.n;
    for (int i = 0; i < y.n - 1; i++) {
      y[i + 1] += y[i] / binary, y[i] %= binary;
    }
    for (; !y.a[y.n - 1] && y.n > 1; y.n--) {
    }
    return y;
  }
  void operator*=(BigInt x) {
    BigInt z = *this;
    *this = z * x;
  }
  void operator*=(int x) {
    BigInt z = *this;
    *this = z * x;
  }
  BigInt operator*(int x) {
    BigInt y;
    y = x;
    return operator*(y);
  }
};
};

using namespace std;
using namespace Integer;

BigInt<2010> a, b;

int main() {
  cin >> a >> b;
  cout << a * b;
  return 0;
}

by 12345limengqi @ 2023-11-04 20:03:35

@2021zjhs005 @heyx0201

谢谢两位大佬指导


by 2021zjhs005 @ 2023-11-04 20:10:15

#include<bits/stdc++.h>
using namespace std;
//首先,长度小于等于 10 的 2000 次方,所以要定的比 2000 大。
char s1[2020],s2[2020];

int a[2020],b[2020],c[4020];//这里别忘改了。
void change(char *s,int a[2010])
{
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        a[len-i-1]=s[i]-'0';
    }
}
void print()
{
    int len=4000;//这里别忘记调成 4000。
    while(c[len]==0&&len>0)
    {
        len--;
    }
    for(int i=len;i>=0;i--)
    {
        printf("%d",c[i]);
    }
}
int main()
{
    scanf("%s%s",s1,s2);
    int len=strlen(s1);
    change(s1,a);
    change(s2,b);
    int len1=strlen(s1);
    int len2=strlen(s2);
    for(int i=0;i<len1;i++)
    {
        for(int j=0;j<len2;j++)
        {
            c[i+j]+=a[i]*b[j];
        }
    }
    for(int i=0;i<4010;i++)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    print();
    return 0;
}

@12345limengqi


|