• (TOJ1192)A + B Problem II


    描述

    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

    输入

    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.

    输出

    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.

    样例输入

    2
    1 2
    112233445566778899 998877665544332211

    样例输出

    Case 1:
    1 + 2 = 3
    
    Case 2:
    112233445566778899 + 998877665544332211 = 1111111111111111110
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<ctype.h>
     4 #include<math.h>
     5 
     6 char a[1001],b[1001];
     7 
     8 void deal(char *s1, char *s2)
     9 {
    10     int len1,len2,i,t,flag;
    11     char *p,*q;
    12     int c[1003]={0};
    13     len1=strlen(s1); len2=strlen(s2);
    14     i=t=flag=0;
    15     printf("%s + ",a);
    16     printf("%s = ",b);
    17     p=strrev(s1);
    18     q=strrev(s2);
    19     while(*p || *q){
    20         if(*p) t+=*p-'0';
    21         if(*q) t+=*q-'0';
    22         t+=flag;
    23         if(t>=10){t=t%10; c[i++]=t; flag=1;}
    24         else {c[i++]=t; flag=0;}
    25         p++; q++;
    26         t=0;
    27     }
    28     if(flag) c[i]+=1;
    29     else i--;
    30 
    31     for(; i>=0; i--){
    32         printf("%d",c[i]);
    33     }
    34 }
    35 
    36 void solve()
    37 {
    38     int i,n,t;
    39     i=1;
    40     scanf("%d",&n);
    41     t=n;
    42     getchar();
    43     while(n--){
    44         scanf("%s",a);  scanf("%s",b);
    45         printf("Case %d:\n",i);
    46         deal(a,b);
    47         if(i!=t) printf("\n\n");
    48         else printf("\n");
    49         i++;
    50         memset(a,'\0',strlen(a)*sizeof(char));
    51         memset(b,'\0',strlen(b)*sizeof(char));
    52     }
    53 }
    54 
    55 int main()
    56 {
    57     solve();
    58     return 0;
    59 }
     
    作者:xueda120
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    Python long() 函数
    Python frozenset() 函数
    java对象和构造方法的区别
    回顾方法和方法调用及加深
    面向过程&面向对象
    稀疏数组
    冒泡排序与八大排序
    Arrays类与常用功能
    二维数组的理解代码
    数组的使用
  • 原文地址:https://www.cnblogs.com/xueda120/p/3091173.html
Copyright © 2020-2023  润新知