Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2115117
  • 博文数量: 249
  • 博客积分: 1305
  • 博客等级: 军士长
  • 技术积分: 4733
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-17 10:37
个人简介

不懂的东西还有很多,随着不断的学习,不懂的东西更多,无法消灭更多不懂的东西,那就不断的充实自己吧。 欢迎关注微信公众号:菜鸟的机器学习

文章分类

全部博文(249)

文章存档

2015年(1)

2014年(4)

2013年(208)

2012年(35)

2011年(1)

分类: C/C++

2013-04-07 22:06:29

A + B Problem 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

 

     题目解析:
        与HDOJ_1000不同,这里包含大整数加法。因此,不能按那个方法进行处理,这里采用了字符串进行大整数的加法。
    
代码如下

  1. #include <stdio.h>
  2. #include <string.h>

  3. #define MAXNUM 1004

  4. //交换
  5. void Swap(char *a, char *b)
  6. {
  7.     char tmp = *a;
  8.     *a = *b;
  9.     *b = tmp;
  10. }
  11. //反转
  12. void reserve(char *a, int len)
  13. {
  14.     int i, j;
  15.     if(a == NULL || len == 0)
  16.     {
  17.         return;
  18.     }

  19.     i = 0;
  20.     j = len - 1;
  21.     while(i < j)
  22.     {
  23.         Swap(&a[i], &a[j]);
  24.         i++;
  25.         j--;
  26.     }
  27. }

  28. int main(int argc, char* argv[])
  29. {
  30.     int t = 0;
  31.     char a[MAXNUM];
  32.     char b[MAXNUM];
  33.     int c[MAXNUM];
  34.     int i = 0, j = 1;
  35.     int count = 1;
  36.     int len_a, len_b, len_c;

  37.     scanf("%d", &t);
  38.     if(t <= 0 || t >= 21)
  39.     {
  40.         return 0;
  41.     }
  42.     for(j = 1; j <= t; j++)
  43.     {
  44.         memset(a, 0, sizeof(a));
  45.         memset(b, 0, sizeof(b));
  46.         memset(c, 0, sizeof(c));

  47.         scanf("%s %s", a, b);

  48.         printf("Case %d:\n", count++);
  49.         printf("%s + %s = ", a, b);
  50.         
  51.         len_a = strlen(a);
  52.         len_b = strlen(b);
  53.         len_c = (len_a > len_b) ? len_a : len_b;

  54.         //反转a和b
  55.         reserve(a, len_a);
  56.         reserve(b, len_b);

  57.         //转为数字
  58.         for(i = 0; i < len_a; i++)
  59.         {
  60.             a[i] -= '0';
  61.         }
  62.         for(i = 0; i < len_b; i++)
  63.         {
  64.             b[i] -= '0';
  65.         }

  66.         //计算
  67.         for(i = 0; i < len_c; i++)
  68.         {
  69.             c[i] = c[i] + a[i] + b[i];
  70.             c[i + 1] += c[i] / 10;
  71.             c[i] %= 10;
  72.         }

  73.         //有进位
  74.         if(c[len_c] > 0)
  75.         {
  76.             len_c++;
  77.         }

  78.         //输出
  79.         for(i = len_c - 1; i >= 0; i--)
  80.         {
  81.             printf("%d",c[i]);
  82.         }
  83.         printf("\n");
  84.         if(j != t)
  85.         {
  86.             printf("\n");
  87.         }
  88.     }
  89.     return 0;
  90. }


阅读(1090) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~