50 悬赏关注!!!

P1001 A+B Problem

gsc15759915601 @ 2023-07-18 15:41:56

#include <bits/stdc++.h>
using namespace std;
char s1[3001],s2[3001];
long long n = 1,a[3001] = {0},b[3001] = {0},c[3001] = {0},jw = 0,len1,len2,len3,zc,cd;
int main(){
    cin>>s1>>s2;
    len1 = strlen(s1);
    len2 = strlen(s2);
    for(long long i = 0;i<len1;i++){
        a[len1-i] = s1[i]-'0';
    }
    for(long long i = 0;i<len2;i++){
        b[len2-i] = s2[i]-'0';
    }
    len3 = max(len1,len2);
    for(long long i = 1;i<=len3+1;i++){
        zc = jw+a[i]+b[i];
        c[i] = zc%10;
        jw = zc/10;
    }
    if(zc){
        len3++;
    }
    for(long long i = len3;i>=1;i--){
        cout<<c[i];
    }
    return 0;
}

by Happy_Doggie @ 2023-07-18 15:44:45

不是您直接输出

cout<<a+b;

不香吗???


by xh39 @ 2023-07-18 15:52:02

@gsc15759915601 请读题: 有负数哦。


by Ayano_ @ 2023-07-18 15:54:16

注意题目中说的是a,b的绝对值小于1e9,考虑一下负数的情况


by xh39 @ 2023-07-18 15:54:47

我知道两种负数的解决方法,一是使用补码,二是将最高位直接设为负。这两种方法都要特殊处理一下负数的输入和输出。


by gsc15759915601 @ 2023-07-18 15:57:12

都关注了


by liuyuxi666 @ 2023-07-19 17:13:04

直接cout<<a+b;


by huangwenze2024 @ 2023-07-24 18:59:18

2


by I_AM_AC_KING @ 2023-07-25 09:19:08

有负数哦~


by kuailehaoge2024 @ 2023-07-25 14:12:24

#include<bits/stdc++.h>
using namespace std;
long long a,b;
int main(){
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

by Entity_303_114514 @ 2023-07-27 11:31:37

@gsc15759915601 这题您想复杂了,可以不用高精度,直接cout<<a+b;就可以了


| 下一页