zoukankan      html  css  js  c++  java
  • HDU_oj_1002 A+B Problem Ⅱ

    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
     
    分析:
    被加数与加数太大,因此无法用普通方式计算,这时应考虑用数组存储,模拟加法运算
    注意点:
    ①数据长度不超过1000
    ②每个结果后面需多空出一行
     
    改了n多次
     1 #include<iostream>
     2 #include<cstring>
     3 #define MAX 1000
     4 
     5 int main()
     6 {
     7     char a[MAX+1],b[MAX+1];
     8     int T;
     9     scanf("%d",&T);
    10     for(int w=1;w<=T;++w)
    11     {
    12         memset(a,0,sizeof(a));
    13         memset(b,0,sizeof(b));
    14         
    15         scanf("%s%s",a,b);
    16         
    17         printf("Case %d:
    ",w);
    18         printf("%s + %s = ",a,b);
    19         
    20         int len1=strlen(a);
    21         int len2=strlen(b);
    22         
    23         int fpla=MAX-(len1>len2 ? len1:len2);
    24         
    25         for(int i=len1-1,k=MAX;i>=0;--i,--k) 
    26             a[k]=(a[i]-'0'),a[i]=0;
    27              
    28         for(int i=len2-1,k=MAX;i>=0;--i,--k) 
    29             b[k]=(b[i]-'0'),b[i]=0; 
    30             
    31         for(int i=MAX;i>=fpla;--i)
    32             a[i]+=b[i]; 
    33             
    34         for(int i=MAX;i>=fpla;--i)
    35         {
    36             int c=a[i]/10;
    37             a[i-1]+=c;
    38             a[i]%=10;
    39         } 
    40         for(int i=fpla+1;i<=MAX;++i)
    41             printf("%d",a[i]);
    42         puts("");
    43         if(w<T)
    44             puts("");
    45     }
    46     return 0;
    47 }
     
     
  • 相关阅读:
    netty ByteToMessageDecoder 分析
    netty 编/解码处理
    MAC 入门
    netty 学习
    php ioc and web rest design
    spring 启动流程
    淘宝美衣人
    ecslipe cdt lib link
    阿里巴巴中间件团队招人了!
    架构师速成-架构目标之伸缩性安全性
  • 原文地址:https://www.cnblogs.com/tenjl-exv/p/7966130.html
Copyright © 2011-2022 走看看