• 1C


    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

    // too young and didn't notice that
    // "you should not process them by using 32-bit integer"
    // and "Output a blank line between two test cases".
     1 #include<stdio.h>
     2 int main()
     3 {
     4     int a, b, t, i;
     5     scanf("%d", &t);
     6     for(i=1;i<=t;i++)
     7     {
     8         scanf("%d %d", &a, &b);
     9         printf("Case %d:
    ", i);
    10         printf("%d + %d = %d
    ", a, b, a+b);
    11         printf("
    ");
    12     }
    13     return 0;
    14 }
    Wrong Answer
    // long int is also 32-bit integer.
    代码省略
    // 不用判断哪个数较长,只要记录答案的长度. 三个数组都要初始化为0!!!
     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 int reverse_add(int *a, int *b, int *c, int al, int bl)
     5 {
     6     int i, sum=0, k, max;
     7     if(al<bl) max=bl;
     8     else      max=al;
     9     k=0;
    10     for(i=0; i<max; i++)
    11     {
    12         *(c+i)=(*(a+i)+*(b+i)+k)%10;
    13         k=(*(a+i)+*(b+i)+k)/10;
    14     }
    15     if(k!=0) 
    16     {
    17         *(c+i)=1;
    18         return max+1;
    19     }
    20     else return max;
    21 }
    22 
    23 int main()
    24 {
    25     char s1[1001], s2[1001];
    26     int t, k, i, length_a, length_b, rmax;
    27     scanf("%d", &t);
    28     for(k=1;k<=t;k++)
    29     {
    30         int a[1001]={0}, b[1001]={0}, c[1001]={0}; /* 三个数组都要初始化 */
    31         scanf("%s %s", s1, s2);
    32         length_a=strlen(s1); length_b=strlen(s2);
    33         for(i=0; i<length_a; i++) a[i]=s1[length_a-1-i]-'0';
    34         for(i=0; i<length_b; i++) b[i]=s2[length_b-1-i]-'0';
    35         rmax=reverse_add(a, b, c, length_a, length_b);
    36         printf("Case %d:
    ", k);
    37         printf("%s + %s = ", s1, s2);
    38         for(i=0; i<rmax; i++) printf("%d", c[rmax-1-i]);
    39         printf("
    ");
    40         if(t>1&&k<t) printf("
    ");
    41     }
    42     return 0;
    43 }
    AC
     
  • 相关阅读:
    Find cmd
    splitFile2SmallFile
    IPC-->PIPO
    Thread and shared lock
    Python Thread related
    linux 文件系统
    linux commands
    关于 gnome3 无法显示应用程序所有界面的反馈
    Windows 网络编程
    常见端口 HTTP代码
  • 原文地址:https://www.cnblogs.com/goldenretriever/p/10356191.html
Copyright © 2020-2023  润新知