zoukankan      html  css  js  c++  java
  • bzoj3767 A+B Problem加强版

    Description

     

    Input

    输入A,B
     

    Output

    输出A+B。

    Sample Input

    1 1

    Sample Output

    2

    HINT

    对于100%的数据,保证 |A| , |B| 不会超过10^(10^7)

    呃……高精a+b和a-b的模板题

    没啥技术含量的……我还以为会有什么--0之类的出现

    #include<cstdio>
    #include<cstring>
    #define N 10000010
    int a[N],b[N],c[N],l1,l2,l3;
    char ch1[N],ch2[N];
    bool m1,m2;
    inline int max(int a,int b){return a>b?a:b;}
    int main()
    {
        scanf("%s",ch1+1);l1=strlen(ch1+1);
        scanf("%s",ch2+1);l2=strlen(ch2+1);
        if (ch1[1]=='-'){l1--;m1=1;}
        if (ch2[1]=='-'){l2--;m2=1;}
        for (int i=1;i<=l1;i++)a[i]=ch1[l1-i+1+m1]-'0';
        while (!a[l1]&&l1>1)l1--;
        if (l1==1&&!a[1])m1=0;
        for (int i=1;i<=l2;i++)b[i]=ch2[l2-i+1+m2]-'0';
        while (!b[l2]&&l2>1)l2--;
        if (l2==1&&!b[1])m2=0;
        l3=max(l1,l2);
        if (m1^m2)
        {
            if (m1)
            {
                for (int i=1;i<=l3;i++)
                {
                    int t=a[i];
                    a[i]=b[i];
                    b[i]=t;
                }
                int t=l1;l1=l2;l2=t;
            }
            bool mrk=0;
            if (l2>l1)mrk=1;
            else if (l1==l2)
            {
                for (int i=l1;i>=1;i--)
                    if (a[i]<b[i]){mrk=1;break;}
                    else if (a[i]>b[i])break;
            }
            if (mrk)
            {
                printf("-");
                for (int i=1;i<=l3;i++)
                {
                    int t=a[i];
                    a[i]=b[i];
                    b[i]=t;
                }
                int t=l1;l1=l2;l2=t;
                 
            }
            for (int i=1;i<=l3;i++)
            {
                a[i]-=b[i];
                if (a[i]<0)
                {
                    a[i]+=10;
                    int p=i+1;
                    while (a[p]==0)
                    {
                        a[p]=9;
                        p++;
                    }
                    a[p]--;
                }
            }
            while (l3>1&&!a[l3])l3--;
            for (int i=l3;i>=1;i--)
                printf("%d",a[i]);
        }else
        {
            if (m1&&m2)printf("-");
            for (int i=1;i<=l3;i++)
            {
                c[i]+=a[i]+b[i];
                if (c[i]>9)
                {
                    c[i]-=10;
                    c[i+1]++;
                }
            }
            if (c[l3+1])l3++;
            for (int i=l3;i>=1;i--)
                printf("%d",c[i]);
        }
    }
    
    ——by zhber,转载请注明来源
  • 相关阅读:
    3秒后页面跳转代码
    数据库 ""和null的在java 持久化中的区别
    去掉标签元素
    hibernate 自动封装
    hql 多对多查询
    javascript 数组
    spring mvc+mybatis整合
    collection映射
    mybatis中one2many
    mybatis中many2one
  • 原文地址:https://www.cnblogs.com/zhber/p/4254574.html
Copyright © 2011-2022 走看看