天天看點

HDU A + B Problem IIA + B Problem II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 307 Accepted Submission(s): 147

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

Sample Output

字元串模拟大數加法

#include <iostream>

#include <string.h>

#include <stdio.h>

<b>using namespace</b> std<b>;</b><b></b>

char sum<b>[</b>1000<b>];</b><b></b>

void  add<b>(</b>string a<b>,</b>string b<b>)</b>

{

     string c<b> ;</b><b></b>

    if<b>(</b>a<b>.</b>length<b>()&lt;</b>b<b>.</b>length<b>())</b>

    {c<b> =</b> a<b> ;</b> a<b> =</b> b<b>;</b> b<b> =</b> c<b>;}</b><b></b>

    int l1<b> =</b> a<b>.</b>length<b>()-</b>1<b>;</b><b></b>

    int l2<b> =</b> b<b>.</b>length<b>()-</b>1<b>;</b><b></b>

    int k<b>=</b>0<b>,</b>i<b>=</b>0<b>;</b>

    memset<b>(</b>sum<b>,</b>0<b>,</b><b>sizeof</b><b>(</b>sum<b>));</b><b></b>

    char t<b>;</b><b></b>

        while<b>(</b>l2<b>!=-</b>1<b>)</b>

        {

           t<b> = (</b>a<b>[</b>l1<b>]-</b>'0'<b>)+(</b>b<b>[</b>l2<b>]-</b>'0'<b>);</b>

           sum<b>[</b>i<b>]= (</b>t<b>+</b>k<b>)%</b>10<b>+</b>'0'<b>;</b>

           k<b> = (</b>t<b>+</b>k<b>)/</b>10<b>;</b>

           l1<b>--;</b>

           l2<b>--;</b>

           i<b>++;</b>

        }<b></b>

         while<b>(</b>l1<b>!=-</b>1<b>)</b>

         {

             t<b> = (</b>a<b>[</b>l1<b>]-</b>'0'<b>);</b>

             sum<b>[</b>i<b>] = (</b>k<b>+</b>t<b>)%</b>10<b>+</b>'0'<b>;</b>

             k<b> = (</b>t<b>+</b>k<b>)/</b>10<b>;</b>

             l1<b>--;</b>

             i<b>++;</b>

         }<b></b>

         if<b>(</b>k<b>!=</b>0<b>)</b>sum<b>[</b>i<b>]=</b>k<b>+</b>'0'<b>;</b>

}<b></b>

int<b> main</b><b>()</b>

    string a<b>,</b>b<b>;</b><b></b>

    int T<b>;</b>

    cin<b>&gt;&gt;</b>T<b>;</b><b></b>

    for<b>(</b><b>int</b> i<b> =</b> 1<b> ;</b> i<b> &lt;=</b> T<b> ;</b>i<b> ++)</b>

    {

        cin<b>&gt;&gt;</b>a<b>&gt;&gt;</b>b<b>;</b>

        add<b>(</b>a<b>,</b>b<b>);</b>

        cout<b>&lt;&lt;</b>"Case "<b>&lt;&lt;</b>i<b>&lt;&lt;</b>":"<b>&lt;&lt;</b>endl<b>;</b>

        cout<b>&lt;&lt;</b>a<b>&lt;&lt;</b>" + "<b>&lt;&lt;</b>b<b>&lt;&lt;</b>" = "<b>;</b><b></b>

        for<b>(</b><b>int</b> j<b> =</b> strlen<b>(</b>sum<b>)-</b>1<b>;</b> j<b> &gt;=</b>0<b>;</b>j<b>--)</b>

            cout<b>&lt;&lt;</b>sum<b>[</b>j<b>];</b>

        if<b>(</b>i<b>&lt;</b>T<b>)</b>

        cout<b>&lt;&lt;</b>endl<b>&lt;&lt;</b>endl<b>;</b><b></b>

        else cout<b>&lt;&lt;</b>endl<b>;</b>

    }<b></b>

    return 0<b>;</b>

}

<b></b>

<b>本文轉自NewPanderKing51CTO部落格,原文連結:</b><b>http://www.cnblogs.com/newpanderking/archive/2011/07/31/2122519.html</b><b> ,如需轉載請自行聯系原作者</b>