40分求助!!!(在P1601A+B Problem(高精)也发了)

P1303 A*B Problem

dongrq_cs @ 2023-04-01 19:05:16

#include <bits/stdc++.h>
using namespace std;
void StrToInt(char s[],int t[]){
    int len = 0;
    for(len = 0;s[len] != '\0';len++){
        ;
    }
    t[0] = len;
    int j = len - 1;
    for(int i = 1;i <= len;i++,j--){
        t[i] = s[j] - '0';
    }
}
void add(int a[],int b[],int c[]){
    for(int i = 1;i <= b[0];i++){   
        int x = 0;
        for(int j = 1;j <= a[0];j++){
            c[i + j - 1] = b[i] * a[j] + x + c[i + j - 1];
            x = c[i + j - 1] / 10;
            c[i + j - 1] %= 10;
        }
        c[a[0] + i] += x;
    }
    int k = a[0] + b[0];
    while(c[k] == 0){
        k--;
    }
    c[0] = k;
}
int main(){
    char d[210],r[210];
    int a[210] = {0},b[210] = {0},c[210] = {0};
    cin >> d >> r;
    StrToInt(d,a);
    StrToInt(r,b);
    add(a,b,c);
    for(int i = c[0];i >= 1;i--){
        cout << c[i];
    }
    return 0;
}

by ShenEric @ 2023-04-01 19:17:06

额......虽然我看不懂,但我有思路


by ShenEric @ 2023-04-01 19:17:32

#include <bits/stdc++.h>
using namespace std;
string a,b;
int aa[10000],ab[10000],c[200000];
void cpy(string s,int as[])
{
    for(int i=0;i<s.size();i++)
    {
        as[s.size()-i-1]=s[i]-'0';
    }
}
void print(int as[])
{
    int i=40001;
    while(as[i]==0)
    {
        i--;
    }
    for(int j=max(i,0);j>=0;j--)
    {
        cout<<as[j];
    }
}
void add()
{
    for(int i=0;i<a.size()+b.size();i++)
    {
        for(int j=i;j>=0;j--)
        {
            if(j<a.size()&&i-j<b.size())
            {
                c[i]+=aa[j]*ab[i-j];
            }
        }
    }
    for(int i=0;i<a.size()+b.size();i++)
    {
        c[i+1]+=c[i]/10;
        c[i]=c[i]%10;
    }
}
int main()
{
    cin>>a>>b;
    cpy(a,aa);
    cpy(b,ab);
    add();
    print(c);
    return 0;
}

by xiaoming007 @ 2023-04-01 19:20:19

@dongrq_cs

#include <bits/stdc++.h>
using namespace std;
void StrToInt(char s[],int t[]){
    int len = 0;
    for(len = 0;s[len] != '\0';len++){
        ;
    }
    t[0] = len;
    int j = len - 1;
    for(int i = 1;i <= len;i++,j--){
        t[i] = s[j] - '0';
    }
}
void add(int a[],int b[],int c[]){
    for(int i = 1;i <= b[0];i++){   
        int x = 0;
        for(int j = 1;j <= a[0];j++){
            c[i + j - 1] = b[i] * a[j] + x + c[i + j - 1];
            x = c[i + j - 1] / 10;
            c[i + j - 1] %= 10;
        }
        c[a[0] + i] += x;
    }
    int k = a[0] + b[0];
    while(c[k] == 0){
        k--;
    }
    c[0] = k;
}
int main(){
    char d[21000],r[21000];
    int a[21000] = {0},b[21000] = {0},c[20010] = {0};
    cin >> d >> r;
    StrToInt(d,a);
    StrToInt(r,b);
    add(a,b,c);bool flag=0;
    for(int i = c[0];i >= 1;i--){
        cout << c[i];flag=1;
    }if(flag==0) cout<<0;
    return 0;
}

by xiaoming007 @ 2023-04-01 19:21:02

@dongrq_cs 死因:

1.没特判0

2.数组小了

这次珂以关注 @HeYilin (


by ShenEric @ 2023-04-01 19:21:48

将每一位的积算出来再累加就行了


by dongrq_cs @ 2023-04-01 19:23:10

@xiaoming007 谢谢 那个号我已经关了,你可以拉别人!


by dongrq_cs @ 2023-04-01 19:23:43

@ShenEric 谢谢 已关注


by GJY201112 @ 2023-04-01 19:24:50

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
    char a1[101],b1[101];
    int a[101],b[101],c[10001],lena,lenb,lenc,i,j,x;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    scanf("%s",a1);
    scanf("%s",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];
    cout<<endl;
}

by ShenEric @ 2023-04-01 19:25:48

?干嘛啊???


by dongrq_cs @ 2023-04-01 19:26:37

非常感谢两位! 已通过!

记录详情


| 下一页