• A + B Problem II


    给定两个整数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
    100 200

    Sample Output

    Case 1:
    1 + 2 = 3
    
    Case 2:
    100 + 200 = 300

    解题思路:大数模板;
     1 #include<iostream>
     2 #include <string.h>
     3 #include <stdio.h>
     4 
     5 using namespace std;
     6 
     7 const int MAX = 1000 + 10;
     8 int N;
     9 int len1,len2,max1;
    10 char a[MAX],b[MAX];
    11 int a1[MAX],b1[MAX];
    12 int sum[MAX];
    13 
    14 int main()
    15 {
    16     cin>>N;
    17 
    18     for(int i = 1;i <= N;i++)
    19     {
    20         memset(a,0,sizeof(a));
    21         memset(a1,0,sizeof(a1));
    22         memset(b,0,sizeof(b));
    23         memset(b1,0,sizeof(b1));
    24         memset(sum,0,sizeof(sum));
    25 
    26         cin>>a>>b;
    27         len1 = strlen(a);
    28         len2 = strlen(b);
    29         for(int i = 0,j = len1 - 1;j >= 0;j--)
    30             a1[i++] = a[j] - '0';
    31         for(int i = 0,j = len2 - 1;j >= 0;j--)
    32             b1[i++] = b[j] - '0';
    33 
    34         int temp = 0;
    35         max1 = len1>len2?len1:len2;
    36         if(len1 > len2)
    37             for(int i = 0;i <= len1;i++)
    38             {
    39                 sum[i] = ( a1[i] + b1[i] + temp) % 10;
    40                 temp = ( a1[i] + b1[i] + temp) / 10;
    41              }
    42         else
    43             for(int i = 0;i <= len2;i++)
    44             {
    45                 sum[i] = ( a1[i] + b1[i] + temp) % 10;
    46                 temp = ( a1[i] + b1[i] + temp) / 10;
    47              }
    48 
    49         printf("Case %d:
    %s + %s = ",i, a , b);
    50 
    51         if(sum[max1] != 0)cout<<sum[max1];
    52         for(int i = max1 - 1;i>=0;i--)
    53            cout<<sum[i];
    54             cout<<endl;
    55         if(i != N)
    56             cout<<endl;
    57     }
    58 
    59 
    60     return 0;
    61 }
  • 相关阅读:
    C知识(#一些笔记)
    我整理的Python规则(2)
    我整理的Python代码规则
    教你如何通俗易懂的了解深度学习知识
    c#中委托和事件(续)(转)
    c#中的委托和事件(转)
    .Net neatupload上传控件实现文件上传的进度条
    C#常用日期格式处理转换[C#日期格式转换大全
    C#string常用函数总结
    数据库08版本如何移动到05的数据中
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7375533.html
Copyright © 2020-2023  润新知