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
--------------------------------------------------------------------------------------------------------------------------
 
//高精度加法,注意格式
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<memory.h>
#define SIZE 1005 int main()
{
int i, n;
int j, k, m;
int index;
int lengthA, lengthB;
char a[SIZE];
char b[SIZE]; int ans[SIZE];
int carryBit; //进位
//freopen("F:\\input.txt","r",stdin);
scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%s%s", a, b); memset(ans, 0, sizeof(ans));
index = 0;
carryBit = 0;
lengthA = strlen(a);
lengthB = strlen(b); lengthA--;
lengthB--;
while ((lengthA >= 0) && (lengthB >= 0))
{
//将char转换为int,并计算和,如果ans大于10,则carryBit为1,否则为0
ans[index] = (a[lengthA] - '0') + (b[lengthB] - '0') + carryBit;
if (ans[index] >= 10)
{
carryBit = ans[index] / 10;
ans[index] %= 10;
}
else
carryBit = 0;
index++;
lengthA--;
lengthB--;
}
//如果A的长度大于B,则将A与carryBit相加
while (lengthA >= 0)
{
ans[index] = (a[lengthA] - '0') + carryBit;
if (ans[index] >= 10)
{
carryBit = ans[index] / 10;
ans[index] %= 10;
}
else
carryBit = 0;
index++;
lengthA--;
}
//如果B的长度大于A,则将B与carryBit相加
while (lengthB >= 0)
{
ans[index] = (b[lengthB] - '0') + carryBit;
if (ans[index] >= 10)
{
carryBit = ans[index] / 10;
ans[index] %= 10;
}
else
carryBit = 0;
index++;
lengthB--;
} if (carryBit != 0)
{
ans[index]++;
index++;
} //去掉多余的零
while (ans[index] == 0)
index--;
printf("Case %d:\n", i + 1);
printf("%s + %s = ", a, b);
//两个零字符串相加输出为0
if (index == -1)
printf("0");
else
{
for (m = index; m >= 0; m--)
printf("%d", ans[m]);
}
printf("\n");
if (i != n - 1)
printf("\n");
}
//freopen("con", "r", stdin);
//system("pause");
return 0;
}

  

ACM1002:A + B Problem II的更多相关文章

  1. HDU 1002:A + B Problem II(大数相加)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. A + B Problem II

    之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历.学习算法有一段时间了,于是想从算法开始自己的博客生涯O(∩_∩)O~~ 今天在网上看了一道大数相加(高精度) ...

  3. hduoj 1002 A + B Problem II

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目描述如下: A + B Problem II Time Limit: 2000/1000 M ...

  4. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  5. nyoj 623 A*B Problem II(矩阵)

    A*B Problem II 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 ACM的C++同学有好多作业要做,最头痛莫过于线性代数了,因为每次做到矩阵相乘的时候,大 ...

  6. HDU 1002 A + B Problem II

    A + B Problem II   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted ...

  7. nyoj 103 A + B problem II

    点击打开链接 A+B Problem II 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. G ...

  8. Train Problem II(卡特兰数+大数乘除)

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. HDU 1002 A + B Problem II(大整数相加)

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

随机推荐

  1. PowerShell管理Azure

    PowerShell第一次连接Azure1.下载Azure SDK,安装azure powershell http://azure.microsoft.com/zh-cn/downloads/?rnd ...

  2. 【转】Linux中/etc/profile,/etc/bashrc,~/.profile,~/.bashrcd的区别

    //因为在原文章中博主说以下内容是网友说的,所以我就只加个转了,找不到原作者 /etc/profile,/etc/bashrc 是系统全局环境变量设定 ~/.profile,~/.bashrc用户家目 ...

  3. Python之条件判断和循环(入门4)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407755.html 本文出自:[Edwin博客园] Python之条件判断和循环 1. Python之if ...

  4. 记录一个比较少用的容器C++ std::bitset

    bitset存储二进制数位. bitset就像一个bool类型的数组一样,但是有空间优化——bitset中的一个元素一般只占1 bit,相当于一个char元素所占空间的八分之一. bitset中的每个 ...

  5. nodejs+postgis实现搜周边

    利用nodejs搭建服务器,并连接PostgreSQL数据库,利用前端传过来的中心点坐标和搜索半径,进行空间查询,实现简单的搜周边,下面是实现流程和nodejs的代码: app.post('/tose ...

  6. php 日志扩展

    今天发现一个比较好的php应用日志扩展,这里先mark一下,回头有空再详细介绍: http://neeke.github.io/SeasLog/

  7. C#读取信息备份

    class Program { static void Main(string[] args) { var wc = new WebClient(); var html = wc.DownloadSt ...

  8. js 实现商品放大镜效果

    知识点,需熟悉下面属性及含义: offsetLeft           //获取元素相对左侧的距离 (计算是从最左侧边框外开始) offsetTop           //获取元素相对顶部的距离 ...

  9. Enum介绍

    public enum Color { RED, YELLOW, BLUE; } 说明: 使用的是enum关键字而不是class 多个枚举变量之间用 逗号 隔开 枚举变量名大写,多个单词之间用 _ 隔 ...

  10. [转]Asp.Net url中文乱码

    一般有3种方法: 1.设置web.config文件 2.传递中文之前,将要传递的中文参数进行编码,在接收时再进行解码.  传递 string Name = "中文参数"; Resp ...