为什么不能全部AC!!!

P1001 A+B Problem

C2024_QAQ @ 2024-07-20 16:41:02

有木有大神看看我的奇妙解法,实在是太难受了

#include<bits/stdc++.h>
#define _MAX 100001
using namespace std;
typedef long long ll;
ll l1,l2;
string s1,s2;
ll a1[_MAX],a2[_MAX],h[_MAX];
int main(){
    cin>>s1>>s2;
    if(s1.size()<s2.size()) swap(s1,s2);
    l1=s1.size(),l2=s2.size();
    for(int i=0;i<l1;i++) a1[i+1]=s1[i]-'0';
    for(int i=l1-l2;i<l1;i++) a2[i+1]=s2[i-l1+l2]-'0';
    for(int i=l1;i>=0;i--){
        if(a1[i]+a2[i]>=10) {
        h[i]=a1[i]+a2[i]-10;
        a1[i-1]+=1;
        if(i-1==0) a1[0]=1;
        }
        else h[i]=a1[i]+a2[i];
    }
    if(h[0]!=0) for(int i=0;i<=l1;i++) cout<<h[i];
    else for(int i=1;i<=l1;i++) cout<<h[i];
    return 0;
}

传送门


by heyichen201209 @ 2024-07-21 13:30:33

@C2024_QAQ 哈?我氧化钙


by yiyuhang123 @ 2024-07-21 21:47:35

#include<bits/stdc++.h>
using namespace std;
string a,b;
int ans[505];
int main(){
    cin>>a>>b;
    if(a.size()<b.size()){
        string c;
        c=a;
        a=b;
        b=c;
    }
    int len=a.size()-b.size();
    for(int i=1;i<=len;i++){
        b='0'+b;
    }
    int mod=0;
    int jinwei=0;
    for(int i=a.size()-1;i>=0;i--){
        int add=(a[i]-'0')+(b[i]-'0')+jinwei;
        mod=add%10;
        ans[i+1]=mod;
        jinwei=add/10;
    }
    ans[0]=jinwei;
    for(int i=0;i<a.size()+1;i++){
        if(i==0){
            if(ans[0]==1){
                cout<<ans[i];   
            }
        }else{
            cout<<ans[i];
        }
    }
    return 0;
}

只能过一半。


上一页 |