本地对了但洛谷50

P1001 A+B Problem

xqaz2wsx3edc @ 2023-07-19 17:27:23

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char ss[100000],oo[100000];
int a[510000],b[510000];
void add(int *a,int *b) {
    if(a[0]<b[0])a[0]=b[0];
    int cr=0;
    for(int i=1; i<=a[0]; i++) {
        a[i]+=b[i]+cr;
        cr=a[i]/10;
        a[i]%=10;
    }
    if(cr) {
        a[0]++;
        a[a[0]]=cr;
    }
}
void t(int *a) {
    scanf("%s",ss);
    a[0]=strlen(ss);
    for(int i=1; i<=a[0]; i++) {
        a[i]=ss[a[0]-i]-'0';
    }
}   
    void o(int *a) {
        for(int i=a[0];i>0; i--) {
            cout<<a[i]; 
        }
    }
int main() {
    t(a);
    t(b);
    add(a,b);
    o(a);
}

by witherbogged @ 2023-11-02 20:58:35

@Huyuhao0215 墙都不扶就服你,有必要吗???


by dulinfan2023 @ 2023-12-23 16:08:18

高精度示例代码

#include<bits/stdc++.h>
using namespace std;
void ck(int k,int carry){
    cout<<"k:"<<k<<" carry:"<<carry<<endl;
}
string add(string s1,string s2){
    int i=s1.length()-1,j=s2.length()-1,carry=0;
    string sum="";
    while(i>=0||j>=0||carry>0){
        int sum1=(i>=0)?s1[i]-'0':0;
        int sum2=(j>=0)?s2[j]-'0':0;
        int k=sum1+sum2+carry;
        carry=k/10;
        k%=10;
        sum=(char)(k+'0')+sum;
        i--,j--;
    }
    return sum;
}
signed main(){
    string s1,s2;
    cin>>s1>>s2;
    string ans=add(s1,s2);
    cout<<ans;
}

上一页 |