问题标题: 酷町堂:高精度乘法

0
0
已解决
张梓沫
张梓沫
资深守护
资深守护

哪位大佬帮忙看一下这个高精度乘法代码哪里有问题?

#include<bits/stdc++.h>
using namespace std;
string a,b;
int main()
{
    cin>>a>>b;
    int la=a.size(),lb=b.size(),aa[100022],bb[100022],cc[100022];
    memset(aa,0,sizeof(aa));
    memset(bb,0,sizeof(bb));
    memset(cc,0,sizeof(cc));
    for(int i=0;i<la;i++)
        aa[la-i]=a[i]-'0';
    for(int i=0;i<lb;i++)
        bb[lb-i]=b[i]-'0';
    int lc=la+lb;
    for(int i=1;i<=la;i++)
        for(int j=1;j<=lb;j++)
            cc[j+i-1]+=a[i]*b[j];
    for(int i=1;i<lc;i++)
    {
        cc[i+1]+=cc[i]/10;
        cc[i]=cc[i]%10;
    }
    while(!cc[lc+1])
        lc--;
    for(int i=lc;i>=1;i--)
        cout<<cc[i];
    return 0;
}

 


1
已采纳
丁子睿
丁子睿
修练者
修练者
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
    char a1[1000],b1[1000];
    int a[1000],b[1000],c[1000],lena,lenb,lenc,i,j,x;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    gets(a1);gets(b1);
    lena=strlen(a1);lenb=strlen(b1);
    for(i=0;i<=lena-1;i++)a[lena-i]=a1[i]-48;
    for(i=0;i<=lenb-1;i++)b[lenb-i]=b1[i]-48;
    for(i=1;i<=lena;i++){
        x=0;
        for(j=1;j<=lenb;j++){
            c[i+j-1]=a[i]*b[j]+x+c[i+j-1];     
            x=c[i+j-1]/10;
           c[i+j-1] %= 10;
        }
        c[i+lenb]=x;
    }
    lenc=lena+lenb;
    while(c[lenc]==0&&lenc>1)lenc--;
    for(i=lenc;i>=1;i--)cout<<c[i];
    return 0;
}

 

0
尹宗鑫
尹宗鑫
新手守护
新手守护
#include<bits/stdc++.h>
using namespace std;
//高精度乘法 2*2==4位
//3*3==6位 La+Lb
string s1,s2;
int a[1001],b[1001],c[2002];
int La,Lb,Lc;// 串长 
void read()
{ int i;
cin>>s1>>s2;
La=s1.length();
Lb=s2.length();
//第1步:s1倒序放在a[]数组 
//个位开始乘
for(i=1;i<=La;i++) 
a[i]=s1[La-i]-'0';
for(i=1;i<=Lb;i++) 
b[i]=s2[Lb-i]-'0';
}
void cf()
{
int i,j,w,x,k;
for(i=1;i<=La;i++)//两道循环 
for(j=1;j<=Lb;j++)//从个位开始
//依次去乘每一位  
{x=a[i]*b[j];//先算乘积 
w=i+j-1;//!!!一定要记住
c[w]=c[w]+x%10;//本位是累和+x的个位 
c[w+1]=c[w+1]+x/10+c[w]/10;
//!w+1位累和有乘法进位+w位的加法进位 
c[w]=c[w]%10;//本位只要个位 
}
//打印
for(i=La+Lb;i>=2;i--) //留下个位 
if(c[i]==0)
    continue;
 else break;
for(k=i;k>=1;k--)
cout<<c[k];
cout<<endl;
}
int main()
{ read();cf();
return 0;
}
0
傅文彬
傅文彬
新手天翼
新手天翼
 

#include<iostream>

 

#include<algorithm>

 

#include<cstdio>

 

#include<cstring>

 

using namespace std;

 

int w[1000010],a[1000010],b[1000010];

 

void he()

 

{

 

int jw;

 

for(int i=1;i<=a[0];i++)

 

{

 

jw=0;

 

for(int j=1;j<=b[0];j++)

 

{

 

w[i+j-1]+=a[i]*b[j]+jw;

 

jw=w[i+j-1]/10;

 

w[i+j-1]%=10;

 

}

 

w[i+b[0]]+=jw;

 

}

 

w[0]=a[0]+b[0];

 

while(w[w[0]]==0&&w[0]!=1)w[0]--;

 

}

 

int main()

 

{

 

char s1[1000010],s2[1000010];

 

cin>>s1>>s2;

 

a[0]=strlen(s1);

 

b[0]=strlen(s2);

 

for(int i=1;i<=a[0];i++)

 

a[i]=s1[a[0]-i]-'0';

 

for(int i=1;i<=b[0];i++)

 

b[i]=s2[b[0]-i]-'0';

 

he();

 

for(int i=w[0];i>=1;i--)cout<<w[i];

 

return 0;

 

}

0
刘承志
刘承志
中级光能
中级光能

#include<limits>
#include<cstring>
#include<list> 
#include<cstdio>
#include<iomanip>
#include<ios> 
#include<iostream>
#include<istream> 
#include<ostream> 
#include<queue> 
#include<set> 
#include<stack> 
#include<stdexcept> 
#include<streambuf> 
#include<string> 
#include<utility> 
#include<cwchar>
#define N 1001
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize(2)
#define MAXSIZE 20
#define MAXOUTSIZE MAXSIZE * 2 + 1
using namespace std;
int main()
{
    char a[MAXSIZE] = {'0'},
         b[MAXSIZE] = {'0'},
         c[MAXOUTSIZE] = {'\0'};
    int a_int[MAXSIZE] = {0},
        b_int[MAXSIZE] = {0},
        c_int[MAXOUTSIZE] = {0};
    cin >> a;
    cin >> b;

    int len_a = strlen(a), len_b = strlen(b);
    memset(a_int, 0, sizeof a_int);
    memset(b_int, 0, sizeof b_int);
    memset(c_int, 0, sizeof c_int);

       for (int i = len_a - 1, j = 0; i >= 0; i--, j++)
    {
        a_int[j] = a[i] - '0';
    }
    for (int i = len_b - 1, j = 0; i >= 0; i--, j++)
    {
        b_int[j] = b[i] - '0';
    }

       for (int i = 0; i < len_a; i++)
    {
        for (int j = 0; j < len_b; j++)
        {
            c_int[i + j] += a_int[i] * b_int[j];
        }
    }

     int ans_len = 0;
    for (int i = 0; i < 2 * MAXSIZE; i++)
    {
        c_int[i + 1] += c_int[i] / 10;
        c_int[i] %= 10;
        ans_len = i;
    }
      while (c_int[ans_len] == 0)
    {
        ans_len--;
    }

    if (ans_len < 0)
    {
        cout << "0";
        return 0;
    }

    for (int i = ans_len; i >= 0; i--)
    {
        c[i] = c_int[i] + '0';
    }

    for (int i = MAXOUTSIZE - 1; i >= 0; i--)
    {
        if ('\0' != c[i])
        {
            cout << c[i];
        }
    }

    return 0;
}

0
我要回答