zoukankan      html  css  js  c++  java
  • HDU 1002 A + B Problem II (大数加法)

    A + B Problem II

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 487411    Accepted Submission(s): 94074

     

    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

    题目大意与分析:

    就是求两个大数的加法,两个用例之间还要有空格,发在这里记一下模板

    #include<bits/stdc++.h>
    
    using namespace std;
    int n,lena,lenb,ai,bi,jin,lenmax,p,q,i,t=0;
    int anss[100000];
    int main()
    {
        cin>>n;
        while(n--)
        {
            t++;
            memset(anss,0,sizeof(anss));
            jin=0;
            string a;
            string b;
            cin>>a;
            cin>>b;
            lena=a.size();
            lenb=b.size();
            lenmax=max(lena,lenb);
            p=lena-1;
            q=lenb-1;
            for(i=lenmax-1;i>=0;i--)
            {
                if(p<0)
                ai='0';
                else
                ai=a[p];
                if(q<0)
                bi='0';
                else
                bi=b[q];        
                anss[i]=(ai-'0'+bi-'0'+jin)%10;
                jin=(ai-'0'+bi-'0'+jin)/10;
                p--;
                q--;
            }
            cout<<"Case "<<t<<':'<<endl;
            cout<<a<<" + "<<b<<" = ";
            if(jin)
            cout<<jin;
            for(i=0;i<lenmax;i++)
            cout<<anss[i];
            cout<<endl;
            if(n)
            cout<<endl;
        }
    }

     

  • 相关阅读:
    进程、线程和协程的区别(转)
    IO多路复用机制(转)
    防火墙及其功能(转)
    TCP连接的建立和终止。
    TCP和UDP细致刻画,区别。
    typename T和class T区别与联系
    TCP UDP的详解开始 ----UNIX网络编程
    关于UNIX网络编程的的OSI,1.7章的总结
    UNIX网络编程daytime服务端和客户端的实现过程
    linux shell脚本执行错误:bad substitution
  • 原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/11324804.html
Copyright © 2011-2022 走看看