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): 147889    Accepted Submission(s): 27893


    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
     
     

    由于处理的数比较大超过了long long 型,所以要变为数组处理比较好!

     解题代码:

    View Code
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 char a[10003], b[10003], c[10003];
     6 
     7 void deal(int lena, int lenb)
     8 {
     9     if (lena > lenb)
    10     {
    11           lenb --;
    12         for (int i = lena - 1; i >= 0; i--)
    13         {
    14             //cout << b[lenb]<<endl;
    15             if(lenb >= 0)
    16             {
    17                 b[i] = b[lenb];
    18                 b[lenb] = '0';
    19                 lenb --;
    20             }
    21             else b[i] = '0';
    22         }
    23         return;
    24     }
    25     if (lena < lenb)
    26     {
    27         lena--;
    28         for (int i = lenb - 1; i >= 0; i --)
    29         {
    30             if (lena >= 0)
    31             {
    32                 a[i] = a[lena];
    33                 a[lena] = '0';
    34                 lena --;
    35             }
    36             else a[i] = '0';
    37         }
    38         return;
    39     }
    40     return ;
    41 }
    42 int main()
    43 {
    44     int n, num = 1;
    45     int lena, lenb;
    46     cin >> n;
    47     while (n--)
    48     {
    49         cin >> a >> b;
    50         lena = strlen (a);
    51         lenb = strlen (b);
    52         printf ("Case %d:\n", num++);
    53         cout << a <<" + " << b<<" = ";        
    54         deal(lena, lenb);
    55         int d = 0;
    56            int j = 0;
    57           for (int i = (lena > lenb ? lena : lenb) - 1; i >= 0; i--)
    58           {
    59                 int temp = a[i] - 48 + b[i] - 48 + d;
    60                 d = temp/10;
    61                 c[j] = temp%10 + 48;
    62                 j++;
    63            }
    64            c[j] = d;
    65            if (d)
    66            {
    67                 c[j] = d + 48;
    68                c[j+1] = 0; 
    69            }
    70         for (int i =j-1; i >= 0; i --)
    71                cout << c[i];
    72           cout << endl;
    73         if (n)
    74             cout <<endl;
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    人与人之间的距离
    Web API之service worker
    css背景图等比例缩放,盒子随背景图等比例缩放
    js节流函数高级版
    vue实现百度搜索下拉提示功能
    vue实现图片点击放大
    css图片宽高相等设置
    sublime常用插件及配置
    css揭秘读书笔记
    webpack2.0简单配置教程
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/2981336.html
Copyright © 2011-2022 走看看