zoukankan      html  css  js  c++  java
  • A+B II

    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
     1 #include<stdio.h>
     2 #include<string.h>
     3 int main()
     4 {
     5     char a[1000],b[1000],c[1001];
     6     int T,T1;
     7     scanf("%d",&T);
     8     T1=T;
     9     getchar();
    10     while(T--)
    11     {
    12         int alen,blen,clen,i,j,k=0,p=0;
    13         memset(a,0,sizeof(a));
    14         memset(b,0,sizeof(b));
    15         memset(c,0,sizeof(c));
    16         scanf("%s%s",a,b);
    17         alen=strlen(a),blen=strlen(b);
    18         for(i=alen-1,j=blen-1;;)
    19         {
    20             c[k]=(a[i]+b[j]-'0'-'0'+p)%10+'0';
    21             p=(a[i]+b[j]-'0'-'0'+p)/10;
    22             if(i==0&&j==0)
    23             {
    24                 if(p>0)
    25                 {    
    26                     c[++k]='1';
    27                     break;
    28                 }
    29                 break;
    30             }
    31             if(i==0)
    32             {
    33                 while(j>0)
    34                 {
    35                     c[++k]=(p+b[--j]-'0')%10+'0';
    36                     p=(p+b[j]-'0')/10;
    37                 }
    38                 break;
    39             }
    40             if(j==0)
    41             {
    42                 while(i>0)
    43                 {
    44                     c[++k]=(p+a[--i]-'0')%10+'0';
    45                     p=(p+a[i]-'0')/10;
    46                 }
    47                 break;
    48             }
    49             i--,j--,k++;
    50         }
    51         printf("Case %d:
    ",T1-T);
    52         for(i=0;i<alen;i++)
    53             printf("%c",a[i]);
    54         printf(" + ");
    55         for(i=0;i<blen;i++)
    56             printf("%c",b[i]);
    57         printf(" = "); 
    58         for(i=k;i>=0;i--)
    59             printf("%c",c[i]);
    60         if(T>0)
    61             printf("
    
    ");
    62         else
    63         printf("
    ");
    64     }
    65 }
  • 相关阅读:
    混杂模式
    消息队列学习
    item21
    消息队列改写
    socket select模型
    EffectiveC++ Item11
    How to read a PCap file from Wireshark with C++
    winsock select学习
    线程安全与可重入函数
    process explorer 查看句柄或者加载的dll
  • 原文地址:https://www.cnblogs.com/a1225234/p/4493368.html
Copyright © 2011-2022 走看看