zoukankan      html  css  js  c++  java
  • 问题 A: a+b

    问题 A: a+b

    时间限制: 1 Sec  内存限制: 32 MB
    提交: 285  解决: 124
    [提交][状态][讨论版][命题人:外部导入]

    题目描述

    实现一个加法器,使其能够输出a+b的值。

    输入

    输入包括两个数a和b,其中a和b的位数不超过1000位。

    输出

    可能有多组测试数据,对于每组数据,
    输出a+b的值。

    样例输入

    6 8
    2000000000 30000000000000000000

    样例输出

    14
    30000000002000000000


    #include<bits/stdc++.h>
    
    using namespace std;
    const int N=1e3+10;
    char a[N],b[N];
    struct bign
    {
        int s[N];
        int len;
        bign(){
            memset(s,0,sizeof(s));
            len=0;
        }
    };
    
    bign change(char str[])
    {
        bign a;
        a.len=strlen(str);
        for(int i=0;i<a.len;i++){
            a.s[i]=str[a.len-i-1]-'0';
        }
        return a;
    }
    
    int comm(bign a,bign b)//大数的比较
    {
        if(a.len>b.len) return 1;
        else if(a.len<b.len) return -1;
        else{
            for(int i=a.len-1;i>=0;i--){
                if(a.s[i]>b.s[i]) return 1;
                else if(a.s[i]<b.s[i]) return -1;
            }
        }
        return 0;
    }
    
    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.s[i]+b.s[i]+carry;
            c.s[c.len++]=temp%10;
            carry=temp/10;
        }
        if(carry!=0) c.s[c.len++]=carry;
        return c;
    }
    int main()
    {
        while(~scanf("%s %s",a,b)){
            bign s1=change(a);
            bign s2=change(b);
            bign c=add(s1,s2);
            for(int i=c.len-1;i>=0;i--){
                printf("%d",c.s[i]);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    windows的端口映射
    windows的ics
    关于windows的右键菜单项 注册表删除
    dig的使用 openwrt
    linux环境变量相关
    Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1
    openwrt ipv6
    ros资料参考
    ipv6的相关参考资料
    supervisor
  • 原文地址:https://www.cnblogs.com/chenchen-12/p/10155408.html
Copyright © 2011-2022 走看看