鱼C论坛

 找回密码
 立即注册
查看: 2877|回复: 11

[已解决]数据结构 大整数加法有待改正,请大家能够帮帮忙。

[复制链接]
发表于 2020-12-28 10:34:56 | 显示全部楼层 |阅读模式
36鱼币
大整数加法运算
窗体底端
【问题描述】
给定测试数据若干组,每组有两个100位以内的正整数,要求程序给出这两个正整数相乘的结果。

【输入格式】
第一行为数据的组数 N,后面有N组测试数据,每组测试数据占一行,每行有两个100位以内的十进制整数。

【输出格式】

输出每一组测试数据的运算结果,每行一个结果。

【示例】
[输入]
6
47858628539074 212821064467
51357484452396 62369401380260
41972493335 769524402008059
16223931314069 936153278101
11775956124 981003321643
4648312830247 63220212506777
[输出]
10185324269616473782483558
3203135561691948572664102960
32298857834403116993786765
15188086483351158930902969
11552252073166227591732
293867324926193383538083919

我用的是vs2019

#include<iostream>
#include<string>
using namespace std;
const int MAX = 100;
class bigint
{
public:
        bigint() { length = 0; }
        bigint(int a[],int n);
        ~bigint() {};
        friend bigint addition(bigint a, bigint b);
        void Printlist();
private:
        int data[MAX];
        int length;
};
bigint::bigint(int a[],int n)
{
        length = n;
        if (length > MAX)throw"yichu";
        n--;
        for (int i = 0; i < length; i++)
                data[length - i] = a[i];


}
bigint addition(bigint A, bigint B)
{
        int flag = 0, i = 0;
        int n, m,len;

        bigint C;
        n = A.length;
        m = B.length;
        len = n > m ? n : m;
        for (i = 0; i < len; i++)
        {
                C.data[i] = A.data[i] + B.data[i];
                if (C.data[i] > 9)
                {
                        C.data[i] -= 10;
                        C.data[i + 1] += 1;
                }
        }
        if (C.data[len] != 0)
            len++;
        return C;
}
void bigint::Printlist()
{
        for (int i = length - 1; i >= 0; i--)
                cout << data[i] << endl;
}
int main()
{
        string a, b;
        int a1[MAX], b1[MAX];

        cout << "请输入1个大数:";
       
                cin >> a;
                cout << endl;
                cout << "请输入第二个大数:";
                cin >> b;
                int lena, lenb;
                lena = a.size();
                lenb = b.size();
                for (int i = 0; i < lena; i++)
                {
                        a1[i] = a[i] - '48';
                }
                for (int i = 0; i < lenb; i++)
                {
                        b1[i] = b[i] - '48';
                }
                bigint A(a1, lena), B(b1, lenb);
                bigint C;
                C = addition(A, B);

                cout << "结果:";
        C.Printlist();
        return 0;
}
最佳答案
2020-12-28 10:34:57
fft算法,最快的乘法了,如果再不行,我也没办法。


  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>

  4. #define        PI        (3.1415926535897932)

  5. typedef struct tagComplex  
  6. {
  7.         double        X;
  8.         double        Y;
  9.        
  10.         tagComplex()
  11.         {
  12.                 X = 0;
  13.                 Y = 0;
  14.         }
  15.        
  16.         tagComplex(const struct tagComplex & c)
  17.         {
  18.                 X = c.X;
  19.                 Y = c.Y;
  20.         }
  21.        
  22.         tagComplex(double x,double y = 0.0)
  23.         {
  24.                 X = x;
  25.                 Y = y;
  26.         }
  27.        
  28.         struct tagComplex & operator = (double x)
  29.         {
  30.                 X = x;
  31.                 Y = 0;
  32.                 return * this;
  33.         }
  34.        
  35.        
  36.         struct tagComplex & operator = (const tagComplex & c)
  37.         {
  38.                 X = c.X;
  39.                 Y = c.Y;
  40.                 return * this;
  41.         }
  42.        
  43.         struct tagComplex & operator += (const tagComplex & c)
  44.         {
  45.                 X += c.X;
  46.                 Y += c.Y;
  47.                 return * this;
  48.         }
  49.        
  50.         struct tagComplex & operator -= (const tagComplex & c)
  51.         {
  52.                 X -= c.X;
  53.                 Y -= c.Y;
  54.                 return * this;
  55.         }
  56.        
  57.         struct tagComplex & operator *= (const tagComplex & c)
  58.         {
  59.                 COMPLEX t = * this;
  60.                 X = t.X * c.X - t.Y * c.Y;
  61.                 Y = t.X * c.Y + t.Y * c.X;
  62.                 return * this;
  63.         }
  64.        
  65.         struct tagComplex & operator /= (double n)
  66.         {
  67.                 X /= n;
  68.                 Y /= n;
  69.                 return * this;
  70.         }
  71.        
  72.         void Mul(const tagComplex & a,const tagComplex & b)
  73.         {
  74.                 * this = a;
  75.                 * this *= b;
  76.         }
  77.        
  78.         operator double()
  79.         {
  80.                 return sqrt(X*X + Y*Y);
  81.         }
  82.        
  83.         operator long()
  84.         {
  85.                 return (long)(X + 0.4);
  86.         }
  87.        
  88.        
  89. }COMPLEX, *LPCOMPLEX;

  90. class CFFT  
  91. {
  92. private:
  93.         CFFT(const CFFT &);
  94.         CFFT & operator = (const CFFT &);
  95.        
  96. private:
  97.         unsigned int BinaryRev(unsigned int ul,int len)
  98.         {
  99.                 unsigned int nRet = 0;
  100.                
  101.                 for(int i = 0 ; i < len ; i ++)
  102.                 {
  103.                         nRet <<= 1;
  104.                         nRet |= (ul & 1);
  105.                         ul >>= 1;
  106.                 }
  107.                
  108.                 return nRet;
  109.         }
  110.        
  111. public:
  112.         CFFT(){};
  113.         ~CFFT(){};
  114.        
  115.         void FFT(LPCOMPLEX fft,long n,bool flag = false)
  116.         {
  117.                 long i,j;
  118.                 long log2n = (long)(log(n)/log(2) + 0.4);
  119.                 COMPLEX omegaTemp;
  120.                
  121.                 for(i = 0 ; i < n ; i ++)
  122.                 {
  123.                         j = BinaryRev(i,log2n);
  124.                        
  125.                         if(i < j)
  126.                         {
  127.                                 omegaTemp = fft[i];
  128.                                 fft[i] = fft[j];
  129.                                 fft[j] = omegaTemp;
  130.                         }
  131.                 }
  132.                
  133.                 for(i = 1 ; i <= log2n ; i++)
  134.                 {
  135.                         long m = (1 << i);
  136.                         COMPLEX omega_m(cos(2*PI/m),sin(2*PI/m));
  137.                        
  138.                         if(flag)
  139.                         {
  140.                                 omega_m.Y = -omega_m.Y;
  141.                         }
  142.                        
  143.                         for(j = 0 ; j < n ; j += m)
  144.                         {
  145.                                 COMPLEX omegaBase(1,0);
  146.                                
  147.                                 for(long k = 0 ; k < m / 2 ; k ++)
  148.                                 {
  149.                                         long loc = j + k + m / 2;
  150.                                        
  151.                                         omegaTemp = omegaBase;
  152.                                         omegaTemp *= fft[loc];
  153.                                        
  154.                                         fft[loc] = fft[j+k];
  155.                                         fft[j+k] += omegaTemp;
  156.                                         fft[loc] -= omegaTemp;
  157.                                        
  158.                                         omegaBase *= omega_m;
  159.                                 }
  160.                         }
  161.                 }
  162.                
  163.                 if(flag)
  164.                 {
  165.                         for(i = 0 ; i < n ; i++)
  166.                         {
  167.                                 fft[i] /= n;
  168.                         }
  169.                 }
  170.         }
  171.        
  172.         void Mul(std::string a,std::string b,std::string & out)
  173.         {
  174.                 long len = 1;
  175.                 long i = a.length() > b.length() ? a.length() : b.length();
  176.                 long j = 0;
  177.                
  178.                 while(i > len)
  179.                 {
  180.                         len <<= 1;
  181.                 }
  182.                
  183.                 len <<= 1;
  184.                
  185.                 char * buffer = new char[(len * sizeof(COMPLEX) * 3)];
  186.                 memset(buffer,0,(len * sizeof(COMPLEX) * 3));
  187.                 LPCOMPLEX f1 = (LPCOMPLEX)buffer;
  188.                 LPCOMPLEX f2 = f1 + len;
  189.                 LPCOMPLEX f3 = f2 + len;
  190.                
  191.                
  192.                
  193.                 for(i = a.length()-1; i >= 0 ; i--)
  194.                 {
  195.                         f1[j++] = a[i] - 0X30;
  196.                 }
  197.                
  198.                 for(j = 0, i = b.length()-1 ; i >= 0 ; i--)
  199.                 {
  200.                         f2[j++] = b[i] - 0X30;
  201.                 }
  202.                
  203.                 FFT(f1,len);
  204.                 FFT(f2,len);
  205.                
  206.                
  207.                 for(i = 0 ; i < len ; i++)
  208.                 {
  209.                         f3[i].Mul(f1[i],f2[i]);
  210.                 }
  211.                
  212.                 FFT(f3,len,true);
  213.                
  214.                 out = "";
  215.                 typedef std::string::iterator SI;
  216.                 SI si = out.begin();
  217.                
  218.                 long num = 0;
  219.                
  220.                 for(i = 0 ; i < len ; i ++)
  221.                 {
  222.                         long n = f3[i];
  223.                         num += n;
  224.                         out.insert(si,(num % 10 + 0X30));
  225.                         num /= 10;               
  226.                 }
  227.                
  228.                 while(out[0] == 0X30 && out.length() > 1)
  229.                 {
  230.                         out.erase(si);
  231.                 }

  232.                 delete [] buffer;
  233.         }
  234. };


  235. int main()
  236. {
  237.         std::string a,b,c;   
  238.     //输入两个大整数
  239.     std::cin >> a >> b;
  240.    
  241.         CFFT fft;
  242.         fft.Mul(a,b,c);
  243.         std::cout << c;

  244.     return 0;
  245. }
复制代码

最佳答案

查看完整内容

fft算法,最快的乘法了,如果再不行,我也没办法。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-28 10:34:57 | 显示全部楼层    本楼为最佳答案   
fft算法,最快的乘法了,如果再不行,我也没办法。


  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>

  4. #define        PI        (3.1415926535897932)

  5. typedef struct tagComplex  
  6. {
  7.         double        X;
  8.         double        Y;
  9.        
  10.         tagComplex()
  11.         {
  12.                 X = 0;
  13.                 Y = 0;
  14.         }
  15.        
  16.         tagComplex(const struct tagComplex & c)
  17.         {
  18.                 X = c.X;
  19.                 Y = c.Y;
  20.         }
  21.        
  22.         tagComplex(double x,double y = 0.0)
  23.         {
  24.                 X = x;
  25.                 Y = y;
  26.         }
  27.        
  28.         struct tagComplex & operator = (double x)
  29.         {
  30.                 X = x;
  31.                 Y = 0;
  32.                 return * this;
  33.         }
  34.        
  35.        
  36.         struct tagComplex & operator = (const tagComplex & c)
  37.         {
  38.                 X = c.X;
  39.                 Y = c.Y;
  40.                 return * this;
  41.         }
  42.        
  43.         struct tagComplex & operator += (const tagComplex & c)
  44.         {
  45.                 X += c.X;
  46.                 Y += c.Y;
  47.                 return * this;
  48.         }
  49.        
  50.         struct tagComplex & operator -= (const tagComplex & c)
  51.         {
  52.                 X -= c.X;
  53.                 Y -= c.Y;
  54.                 return * this;
  55.         }
  56.        
  57.         struct tagComplex & operator *= (const tagComplex & c)
  58.         {
  59.                 COMPLEX t = * this;
  60.                 X = t.X * c.X - t.Y * c.Y;
  61.                 Y = t.X * c.Y + t.Y * c.X;
  62.                 return * this;
  63.         }
  64.        
  65.         struct tagComplex & operator /= (double n)
  66.         {
  67.                 X /= n;
  68.                 Y /= n;
  69.                 return * this;
  70.         }
  71.        
  72.         void Mul(const tagComplex & a,const tagComplex & b)
  73.         {
  74.                 * this = a;
  75.                 * this *= b;
  76.         }
  77.        
  78.         operator double()
  79.         {
  80.                 return sqrt(X*X + Y*Y);
  81.         }
  82.        
  83.         operator long()
  84.         {
  85.                 return (long)(X + 0.4);
  86.         }
  87.        
  88.        
  89. }COMPLEX, *LPCOMPLEX;

  90. class CFFT  
  91. {
  92. private:
  93.         CFFT(const CFFT &);
  94.         CFFT & operator = (const CFFT &);
  95.        
  96. private:
  97.         unsigned int BinaryRev(unsigned int ul,int len)
  98.         {
  99.                 unsigned int nRet = 0;
  100.                
  101.                 for(int i = 0 ; i < len ; i ++)
  102.                 {
  103.                         nRet <<= 1;
  104.                         nRet |= (ul & 1);
  105.                         ul >>= 1;
  106.                 }
  107.                
  108.                 return nRet;
  109.         }
  110.        
  111. public:
  112.         CFFT(){};
  113.         ~CFFT(){};
  114.        
  115.         void FFT(LPCOMPLEX fft,long n,bool flag = false)
  116.         {
  117.                 long i,j;
  118.                 long log2n = (long)(log(n)/log(2) + 0.4);
  119.                 COMPLEX omegaTemp;
  120.                
  121.                 for(i = 0 ; i < n ; i ++)
  122.                 {
  123.                         j = BinaryRev(i,log2n);
  124.                        
  125.                         if(i < j)
  126.                         {
  127.                                 omegaTemp = fft[i];
  128.                                 fft[i] = fft[j];
  129.                                 fft[j] = omegaTemp;
  130.                         }
  131.                 }
  132.                
  133.                 for(i = 1 ; i <= log2n ; i++)
  134.                 {
  135.                         long m = (1 << i);
  136.                         COMPLEX omega_m(cos(2*PI/m),sin(2*PI/m));
  137.                        
  138.                         if(flag)
  139.                         {
  140.                                 omega_m.Y = -omega_m.Y;
  141.                         }
  142.                        
  143.                         for(j = 0 ; j < n ; j += m)
  144.                         {
  145.                                 COMPLEX omegaBase(1,0);
  146.                                
  147.                                 for(long k = 0 ; k < m / 2 ; k ++)
  148.                                 {
  149.                                         long loc = j + k + m / 2;
  150.                                        
  151.                                         omegaTemp = omegaBase;
  152.                                         omegaTemp *= fft[loc];
  153.                                        
  154.                                         fft[loc] = fft[j+k];
  155.                                         fft[j+k] += omegaTemp;
  156.                                         fft[loc] -= omegaTemp;
  157.                                        
  158.                                         omegaBase *= omega_m;
  159.                                 }
  160.                         }
  161.                 }
  162.                
  163.                 if(flag)
  164.                 {
  165.                         for(i = 0 ; i < n ; i++)
  166.                         {
  167.                                 fft[i] /= n;
  168.                         }
  169.                 }
  170.         }
  171.        
  172.         void Mul(std::string a,std::string b,std::string & out)
  173.         {
  174.                 long len = 1;
  175.                 long i = a.length() > b.length() ? a.length() : b.length();
  176.                 long j = 0;
  177.                
  178.                 while(i > len)
  179.                 {
  180.                         len <<= 1;
  181.                 }
  182.                
  183.                 len <<= 1;
  184.                
  185.                 char * buffer = new char[(len * sizeof(COMPLEX) * 3)];
  186.                 memset(buffer,0,(len * sizeof(COMPLEX) * 3));
  187.                 LPCOMPLEX f1 = (LPCOMPLEX)buffer;
  188.                 LPCOMPLEX f2 = f1 + len;
  189.                 LPCOMPLEX f3 = f2 + len;
  190.                
  191.                
  192.                
  193.                 for(i = a.length()-1; i >= 0 ; i--)
  194.                 {
  195.                         f1[j++] = a[i] - 0X30;
  196.                 }
  197.                
  198.                 for(j = 0, i = b.length()-1 ; i >= 0 ; i--)
  199.                 {
  200.                         f2[j++] = b[i] - 0X30;
  201.                 }
  202.                
  203.                 FFT(f1,len);
  204.                 FFT(f2,len);
  205.                
  206.                
  207.                 for(i = 0 ; i < len ; i++)
  208.                 {
  209.                         f3[i].Mul(f1[i],f2[i]);
  210.                 }
  211.                
  212.                 FFT(f3,len,true);
  213.                
  214.                 out = "";
  215.                 typedef std::string::iterator SI;
  216.                 SI si = out.begin();
  217.                
  218.                 long num = 0;
  219.                
  220.                 for(i = 0 ; i < len ; i ++)
  221.                 {
  222.                         long n = f3[i];
  223.                         num += n;
  224.                         out.insert(si,(num % 10 + 0X30));
  225.                         num /= 10;               
  226.                 }
  227.                
  228.                 while(out[0] == 0X30 && out.length() > 1)
  229.                 {
  230.                         out.erase(si);
  231.                 }

  232.                 delete [] buffer;
  233.         }
  234. };


  235. int main()
  236. {
  237.         std::string a,b,c;   
  238.     //输入两个大整数
  239.     std::cin >> a >> b;
  240.    
  241.         CFFT fft;
  242.         fft.Mul(a,b,c);
  243.         std::cout << c;

  244.     return 0;
  245. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-28 21:32:37 | 显示全部楼层
题目是求两数的积,不是和

  1. #include <iostream>
  2. #include <string.h>

  3. #define        _BASE_SIZE        (1024)

  4. class CBigInt
  5. {
  6. public:
  7.         CBigInt()
  8.         {
  9.                 _num = new char[_BASE_SIZE];
  10.                 memset(_num,0,_BASE_SIZE);
  11.                 _size = _BASE_SIZE;
  12.                 _len = 0;
  13.         }

  14.         CBigInt(const CBigInt & n)
  15.         {
  16.                 _num = new char[_BASE_SIZE];
  17.                 memset(_num,0,_BASE_SIZE);
  18.                 _size = _BASE_SIZE;
  19.                 *this = n;               
  20.         }

  21.         ~CBigInt()
  22.         {
  23.                 if(_num != NULL)
  24.                 {
  25.                         delete [] _num;
  26.                 }
  27.         }

  28.         CBigInt & operator = (const CBigInt & n)
  29.         {
  30.                 memcpy(_num,n._num,_size);
  31.                 _len = n._len;
  32.                 return * this;
  33.         }

  34.         friend CBigInt operator + (const CBigInt & a,const CBigInt b)
  35.         {
  36.                 CBigInt n = a;
  37.                 n += b;
  38.                 return n;
  39.         }

  40.         friend CBigInt operator * (const CBigInt & a,const CBigInt b)
  41.         {
  42.                 CBigInt n = a;
  43.                 n *= b;
  44.                 return n;
  45.         }

  46.         friend std::istream & operator >> (std::istream & in, CBigInt & n)
  47.         {
  48.                 char buffer[1024] = {0};

  49.                 std::cin >> buffer;

  50.                 int len = strlen(buffer);

  51.                 for(int i=len-1,j=0 ; i>=0 ; i--)
  52.                 {
  53.                         n._num[j++] = buffer[i] - '0';
  54.                 }

  55.                 n._len = len;

  56.                 return in;
  57.         }

  58.         friend std::ostream & operator << (std::ostream & out,const CBigInt & n)
  59.         {
  60.                 for(int i=n._len- 1 ; i>=0 ; i--)
  61.                 {
  62.                         char ch = (n._num[i] + '0');
  63.                         std::cout << ch;
  64.                 }

  65.                 return out;
  66.         }
  67. private:
  68.        
  69.         CBigInt & operator += (const CBigInt & n)
  70.         {
  71.                 char value = 0;
  72.                 char carry = 0;
  73.                 int len = _len >= n._len ? _len : n._len;

  74.                 for(int i=0 ; i<len ; i++)
  75.                 {
  76.                         value = carry;

  77.                         if(i<_len)
  78.                         {
  79.                                 value += _num[i];
  80.                         }

  81.                         if(i<n._len)
  82.                         {
  83.                                 value += n._num[i];
  84.                         }
  85.                         carry = value / 10;
  86.                         _num[i] = value % 10;
  87.                 }

  88.                 if(carry > 0 && i+1 < n._size)
  89.                 {
  90.                         _num[i++] =  carry;
  91.                 }

  92.                 _len = i;
  93.                 return * this;
  94.         }
  95.        
  96.         CBigInt & operator *= (char n)
  97.         {
  98.                 char value = 0;
  99.                 char carry = 0;

  100.                 for(int i=0 ; i<_len ; i++)
  101.                 {
  102.                         value = carry;
  103.                         value += n * _num[i];
  104.                         carry = value / 10;
  105.                         _num[i] = value % 10;
  106.                 }

  107.                 if(carry > 0 && i+1 < _size)
  108.                 {
  109.                         _num[i++] = carry;
  110.                 }

  111.                 _len = i;
  112.                
  113.                 return * this;
  114.         }

  115.         CBigInt & operator *= (const CBigInt & n)
  116.         {
  117.                 CBigInt tempnum;
  118.                 CBigInt addnum;

  119.                 for(int i=0; i<n._len; i++)
  120.                 {
  121.                         tempnum = *this;
  122.                         tempnum *= n._num[i];
  123.                         tempnum <<= i;
  124.                         addnum += tempnum;
  125.                 }
  126.                 *this = addnum;

  127.                 return * this;
  128.         }

  129.         CBigInt & operator <<= (int i)
  130.         {
  131.                 if(i + _len + 1 < _size)
  132.                 {
  133.                         memcpy(_num+i,_num,_len);
  134.                         memset(_num,0,i);
  135.                         _len += i;
  136.                 }

  137.                 return * this;
  138.         }

  139. private:
  140.         char * _num;
  141.         int _size;
  142.         int _len;
  143. };

  144. int main()
  145. {
  146.         CBigInt a,b;
  147.        
  148.         //输入两个大整数
  149.         std::cin >> a >> b;
  150.        
  151.         //输出两数之和
  152.         std::cout << a + b;
  153.         //输出两数之积
  154.         std::cout << a * b;

  155.         return 0;
  156. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-29 08:45:43 | 显示全部楼层
请问,为什么提示i未定义呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-29 08:49:13 | 显示全部楼层
白砂糖 发表于 2020-12-29 08:45
请问,为什么提示i未定义呢?

c++支持变量即用即定义啊,你用的什么编译器?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-29 08:59:44 | 显示全部楼层
白砂糖 发表于 2020-12-29 08:45
请问,为什么提示i未定义呢?

实在不行,就把变量先定义吧
  1. #include <iostream>
  2. #include <string.h>

  3. #define        _BASE_SIZE        (1024)

  4. class CBigInt
  5. {
  6. public:
  7.         CBigInt()
  8.         {
  9.                 _num = new char[_BASE_SIZE];
  10.                 memset(_num,0,_BASE_SIZE);
  11.                 _size = _BASE_SIZE;
  12.                 _len = 0;
  13.         }

  14.         CBigInt(const CBigInt & n)
  15.         {
  16.                 _num = new char[_BASE_SIZE];
  17.                 memset(_num,0,_BASE_SIZE);
  18.                 _size = _BASE_SIZE;
  19.                 *this = n;               
  20.         }

  21.         ~CBigInt()
  22.         {
  23.                 if(_num != NULL)
  24.                 {
  25.                         delete [] _num;
  26.                 }
  27.         }

  28.         CBigInt & operator = (const CBigInt & n)
  29.         {
  30.                 memcpy(_num,n._num,_size);
  31.                 _len = n._len;
  32.                 return * this;
  33.         }

  34.         friend CBigInt operator + (const CBigInt & a,const CBigInt b)
  35.         {
  36.                 CBigInt n = a;
  37.                 n += b;
  38.                 return n;
  39.         }

  40.         friend CBigInt operator * (const CBigInt & a,const CBigInt b)
  41.         {
  42.                 CBigInt n = a;
  43.                 n *= b;
  44.                 return n;
  45.         }

  46.         friend std::istream & operator >> (std::istream & in, CBigInt & n)
  47.         {
  48.                 char buffer[1024] = {0};
  49.                                 int i,j;
  50.                                 int len;

  51.                 std::cin >> buffer;

  52.                 len = strlen(buffer);

  53.                 for(i=len-1,j=0 ; i>=0 ; i--)
  54.                 {
  55.                         n._num[j++] = buffer[i] - '0';
  56.                 }

  57.                 n._len = len;

  58.                 return in;
  59.         }

  60.         friend std::ostream & operator << (std::ostream & out,const CBigInt & n)
  61.         {
  62.                                 int i;
  63.                                 char ch;

  64.                 for(i=n._len- 1 ; i>=0 ; i--)
  65.                 {
  66.                         ch = (n._num[i] + '0');
  67.                         std::cout << ch;
  68.                 }

  69.                 return out;
  70.         }
  71. private:
  72.         
  73.         CBigInt & operator += (const CBigInt & n)
  74.         {               
  75.                                 int i;
  76.                 char value = 0;
  77.                 char carry = 0;
  78.                 int len = _len >= n._len ? _len : n._len;

  79.                 for(i=0 ; i<len ; i++)
  80.                 {
  81.                         value = carry;

  82.                         if(i<_len)
  83.                         {
  84.                                 value += _num[i];
  85.                         }

  86.                         if(i<n._len)
  87.                         {
  88.                                 value += n._num[i];
  89.                         }
  90.                         carry = value / 10;
  91.                         _num[i] = value % 10;
  92.                 }

  93.                 if(carry > 0 && i+1 < n._size)
  94.                 {
  95.                         _num[i++] =  carry;
  96.                 }

  97.                 _len = i;
  98.                 return * this;
  99.         }
  100.         
  101.         CBigInt & operator *= (char n)
  102.         {
  103.                                 int i;
  104.                 char value = 0;
  105.                 char carry = 0;

  106.                 for(i=0 ; i<_len ; i++)
  107.                 {
  108.                         value = carry;
  109.                         value += n * _num[i];
  110.                         carry = value / 10;
  111.                         _num[i] = value % 10;
  112.                 }

  113.                 if(carry > 0 && i+1 < _size)
  114.                 {
  115.                         _num[i++] = carry;
  116.                 }

  117.                 _len = i;
  118.                
  119.                 return * this;
  120.         }

  121.         CBigInt & operator *= (const CBigInt & n)
  122.         {
  123.                 CBigInt tempnum;
  124.                 CBigInt addnum;
  125.                                 int i;

  126.                 for(i=0; i<n._len; i++)
  127.                 {
  128.                         tempnum = *this;
  129.                         tempnum *= n._num[i];
  130.                         tempnum <<= i;
  131.                         addnum += tempnum;
  132.                 }
  133.                 *this = addnum;

  134.                 return * this;
  135.         }

  136.         CBigInt & operator <<= (int i)
  137.         {
  138.                 if(i + _len + 1 < _size)
  139.                 {
  140.                         memcpy(_num+i,_num,_len);
  141.                         memset(_num,0,i);
  142.                         _len += i;
  143.                 }

  144.                 return * this;
  145.         }

  146. private:
  147.         char * _num;
  148.         int _size;
  149.         int _len;
  150. };

  151. int main()
  152. {
  153.         CBigInt a,b;
  154.         
  155.         //输入两个大整数
  156.         std::cin >> a >> b;
  157.         
  158.         //输出两数之和
  159.         std::cout << a + b;
  160.         //输出两数之积
  161.         std::cout << a * b;

  162.         return 0;
  163. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-29 10:21:54 | 显示全部楼层
因为是在题库上提交,要求要在10000毫秒内完成,应该怎么办呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-29 11:13:03 | 显示全部楼层
谢谢你,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-29 11:13:41 | 显示全部楼层
我用的是vs2019,运行不出来
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-29 11:13:53 | 显示全部楼层
白砂糖 发表于 2020-12-29 10:21
因为是在题库上提交,要求要在10000毫秒内完成,应该怎么办呢?

100位数的乘法,怎么也要不了10秒钟吧,你在什么平台上提交的?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-29 14:12:20 | 显示全部楼层
感谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-30 17:13:04 | 显示全部楼层
数据结构课程设计在线平台
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 16:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表