zoukankan      html  css  js  c++  java
  • HDU 1002

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002

    Problem Description
    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

    Input
    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

    Output
    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

    Sample Input
    2
    1 2
    112233445566778899 998877665544332211

    Sample Output
    Case 1:
    1 + 2 = 3

    Case 2:
    112233445566778899 + 998877665544332211 = 1111111111111111110

    题意:

    给出 $A,B$,求 $A+B$。

    题解:

    数据范围太大,需要手工模拟加法。

    AC代码:

    #include<stdio.h>
    #include<string.h>
    #define max(a,b) ((a>b)?a:b)
    int main()
    {
        char ta[5001],tb[5001];
        int a[5001],b[5001],sum[5002],n,t,i,lena,lenb,lenmax;
        scanf("%d",&n);
        for(t=1;t<=n;t++)
        {
            scanf("%s %s",ta,tb);
            lena=strlen(ta);
            lenb=strlen(tb);
            lenmax=max(lena,lenb);
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b)); 
            memset(sum,0,sizeof(sum));
            for(i=0;i<=lena-1;i++) a[i]=ta[lena-1-i]-'0';
            for(i=0;i<=lenb-1;i++) b[i]=tb[lenb-1-i]-'0';
            for(i=0;i<=lenmax-1;i++)
            {
                sum[i]+=(a[i]+b[i]);
                if(sum[i]>=10)
                {
                    if(i==lenmax-1) lenmax++;
                    sum[i]-=10;
                    sum[i+1]++;
                }
            }
            printf("Case %d:
    ",t);
            printf("%s + %s = ",ta,tb);
            for(i=lenmax-1;i>=0;i--) printf("%d",sum[i]); printf("
    ");
            if(t<n) printf("
    ");
        }
    }
    View Code

    (默默把当年刚入坑时敲得C语言代码折叠)拿出我们的高精度板子!

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1005;
    
    struct BigInt
    {
        int len,d[maxn];
    
        void clean(){while(len>1 && !d[len-1]) len--;}
        void output(){for(int i=len-1;i>=0;i--) printf("%d",d[i]);}
        string str()const
        {
            string s;
            for(int i=0;i<len;i++) s+=d[len-1-i]+'0';
            return s;
        }
    
        BigInt(){memset(d,0,sizeof(d));len=1;}
        BigInt(int num){*this=num;}
        BigInt(char* num){*this=num;}
    
        bool operator<(const BigInt& oth)const
        {
            if(len!=oth.len) return len<oth.len;
            for(int i=len-1;i>=0;i--) if(d[i]!=oth.d[i]) return d[i]<oth.d[i];
            return false;
        }
        bool operator>(const BigInt& oth)const{return oth<*this;}
        bool operator<=(const BigInt& oth)const{return !(oth<*this);}
        bool operator>=(const BigInt& oth)const{return !(*this<oth);}
        bool operator!=(const BigInt& oth)const{return oth<*this || *this<oth;}
        bool operator==(const BigInt& oth)const{return !(oth<*this) && !(*this<oth);}
    
        BigInt operator=(const char* num)
        {
            memset(d,0,sizeof(d));
            len=strlen(num);
            for(int i=0;i<len;i++) d[i]=num[len-1-i]-'0';
            clean();
            return *this;
        }
        BigInt operator=(int num)
        {
            char s[20];
            sprintf(s,"%d",num);
            return *this=s;
        }
        BigInt operator+(const BigInt& oth)const
        {
            BigInt c;
            c.len=max(len,oth.len);
            for(int i=0;i<=c.len;i++) c.d[i]=0;
            for(int i=0;i<c.len;i++)
            {
                c.d[i]+=(i<len?d[i]:0)+(i<oth.len?oth.d[i]:0);
                c.d[i+1]+=c.d[i]/10;
                c.d[i]%=10;
            }
            c.len+=(c.d[c.len]>0);
            c.clean();
            return c;
        }
        BigInt operator-(const BigInt& oth)const
        {
            BigInt c=*this;
            if(c<oth) printf("Produce negative number!
    ");
            int i;
            for(i=0;i<oth.len;i++)
            {
                c.d[i]-=oth.d[i];
                if(c.d[i]<0) c.d[i]+=10, c.d[i+1]--;
            }
            while(c.d[i]<0) c.d[i++]+=10, c.d[i]--;
            c.clean();
            return c;
        }
        BigInt operator*(const BigInt& oth)const
        {
            BigInt c;
            for(int i=0;i<len;i++) for(int j=0;j<oth.len;j++) c.d[i+j]+=d[i]*oth.d[j];
            for(int i=0;i<len+oth.len || !c.d[i];c.len=++i) c.d[i+1]+=c.d[i]/10, c.d[i]%=10;
            c.clean();
            return c;
        }
        BigInt operator/(const BigInt& oth)const
        {
            BigInt c=*this, r=0;
            for(int i=0;i<len;i++)
            {
                r=r*10+c.d[len-1-i];
                int j;
                for(j=0;j<10;j++) if(r<oth*(j+1)) break;
                c.d[len-1-i]=j;
                r=r-oth*j;
            }
            c.clean();
            return c;
        }
        BigInt operator%(const BigInt& oth)
        {
            BigInt r=0;
            for(int i=0;i<len;i++)
            {
                r=r*10+d[len-1-i];
                int j;
                for(j=0;j<10;j++) if(r<oth*(j+1)) break;
                r=r-oth*j;
            }
            return r;
        }
        BigInt operator+=(const BigInt& oth)
        {
            *this=*this+oth;
            return *this;
        }
        BigInt operator*=(const BigInt& oth)
        {
            *this=*this*oth;
            return *this;
        }
        BigInt operator-=(const BigInt& oth)
        {
            *this=*this-oth;
            return *this;
        }
        BigInt operator/=(const BigInt& oth)
        {
            *this=*this/oth;
            return *this;
        }
    }a,b;
    
    char A[maxn],B[maxn];
    int main()
    {
        int T;
        cin>>T;
        for(int kase=1;kase<=T;kase++)
        {
            scanf("%s%s",A,B);
            a=A, b=B;
            printf("Case %d:
    ",kase);
            a.output();
            printf(" + ");
            b.output();
            printf(" = ");
            (a+b).output();
            printf("%s",kase<T?"
    
    ":"
    ");
        }
    }
  • 相关阅读:
    C语言截取从某位置开始指定长度子字符串方法
    vim:放弃hjkl
    vim资源
    PHP和.NET通用的加密解密函数类,均使用3DES加解密 .
    Java与.NET DES加密解密互转
    案例:使用正则表达式的爬虫
    爬虫的正则表达式re模块
    爬虫中Requests模块
    Oracle系列十一 数据处理
    爬虫urllib2 的异常错误处理URLError和HTTPError
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804189.html
Copyright © 2011-2022 走看看