大佬们,求救!!!

P1001 A+B Problem

DreamCHN @ 2024-02-22 15:30:58

压位高精#8WA了

#include<bits/stdc++.h>
#define p 8
#define carry 100000000
using namespace std;  
const int Maxn=50001;  
char s1[Maxn],s2[Maxn];  
int a[Maxn],b[Maxn],ans[Maxn];  
int change(char s[],int n[])   
{  
    char temp[Maxn];   
    int len=strlen(s+1),cur=0;  
    while(len/p){  
        strncpy(temp,s+len-p+1,p);
        n[++cur]=atoi(temp); 
        len-=p;
    }  
    if(len){
        memset(temp,0,sizeof(temp));  
        strncpy(temp,s+1,len);  
        n[++cur]=atoi(temp);   
    }  
    return cur;
}  
int add(int a[],int b[],int c[],int l1,int l2)  
{  
    int x=0,l3=max(l1,l2);  
    for(int i=1;i<=l3;i++){  
        c[i]=a[i]+b[i]+x;  
        x=c[i]/carry;
        c[i]%=carry;  
    }  
    while(x>0){c[++l3]=x%10;x/=10;}  
    return l3;
}  
void print(int a[],int len)  
{   
    printf("%d",a[len]);
    for(int i=len-1;i>=1;i--)printf("%0*d",p,a[i]);
    printf("\n");  
}  
int main()  
{
    scanf("%s%s",s1+1,s2+1);
    int la=change(s1,a);
    int lb=change(s2,b);
    int len=add(a,b,ans,la,lb);    
    print(ans,len);
}  

by Y_zhao111 @ 2024-02-22 15:44:18

@DreamCHN 这是普通的 \texttt{A+B}


by Y_zhao111 @ 2024-02-22 15:50:08

@DreamCHN 你没考虑负数


by DreamCHN @ 2024-02-22 15:50:39

@Y_zhao 好的,谢谢


by Y_zhao111 @ 2024-02-22 16:01:28

@DreamCHN 互关


by GeorgeCHN @ 2024-04-02 18:17:54

咱们都是dream骨灰级粉,我就问你: 直接cout<<a+b 这样不香吗?


by GeorgeCHN @ 2024-04-28 15:19:44

如果你实在想尝试高精,你可以这样:

示例15+15

下标 0 1

0 * 1 5

1 * 1 5

把数字拆位存进数组,再一位位加,记得进位


by GeorgeCHN @ 2024-04-28 15:20:42

@DreamCHN 求互关!


by DreamCHN @ 2024-05-17 20:45:49

@GeorgeCHN ok


|