题解 | #a+b#

a+b

https://www.nowcoder.com/practice/4c39c984ea3848b48e111b8e71ec1dd4

#include <iostream>
#include <cstring>

using namespace std;

struct bign {
    int d[1000];
    int len;

    bign() {
        len = 0;
        memset(d, 0, sizeof(d));
    }
};

bign add(bign a, bign b) {
    bign c;
    int carry = 0;//进位
    for (int i = 0; i < a.len || i < b.len; i++) {
        int temp = a.d[i] + b.d[i] + carry;
        c.d[c.len++] = temp % 10;
        carry = temp / 10;
    }
    if (carry != 0) {
        c.d[c.len++] = carry;
    }
    return c;
}


bign change(char s[]) {
    bign c;
    string str = s;
    c.len = str.size();
    for (int i = 0; i < c.len; i++) {  //倒着赋值
        c.d[i] = str[c.len - i - 1] - '0';
    }
    return c;
}


void print(bign a) {
    for (int i = a.len - 1; i >= 0; i--) {
        printf("%d", a.d[i]);
    }
    printf("\n");
}

int main() {
    char s1[1000];
    char s2[1000];
    scanf("%s", s1);
    scanf("%s", s2);
    bign a = change(s1);
    bign b = change(s2);
    bign c = add(a, b);
    print(c);
    return 0;
}

全部评论

相关推荐

10-25 00:32
香梨想要offer:感觉考研以后好好学 后面能乱杀,目前这简历有点难
点赞 评论 收藏
分享
爱看电影的杨桃allin春招:我感觉你在炫耀
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务