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. Anaconda套件,精簡版miniconda

    雖然Anaconda會預先安裝豐富的套件模組,尤其是在數據科學領域方面,有非常豐富的寶藏, 大多範例或教學或許為了節省後續的麻煩,不解釋為什麼,直接就安裝Anaconda 就對了: 但是大部份的模組套 ...

  2. Oracle 内存使用建议性能视图

    下面三个查询结果均可查询出随着内存参数设置的变化性能的变化情况,对oracle数据库内存的设置有一定的建议和指导作用. select t.SGA_SIZE,t.ESTD_DB_TIME_FACTOR ...

  3. CentOS 7 Nginx+PHP环境搭建!

    1.Nginx 安装 rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx. ...

  4. ZT 设计模式六大原则(5):迪米特法则

    转贴: 设计模式六大原则(5):迪米特法则   原帖子的后续评论里面很多值得仔细去看 切记!像21楼 21楼 chenshufei2 2012-09-23 12:47发表 [回复] 上个例子,就是方法 ...

  5. 如何在Windows 7/8/10中使用热键来调整音量?

    有时,您需要一个热键来调整Windows PC中的音量.例如:播放全屏视频或游戏时需要调整音量. 有一个简单的方法可以做到: 安装并运行Perfect Hotkey软件.   配置键盘快捷键以进行音量 ...

  6. OC 指向指针的指针

    #import <Foundation/Foundation.h> void changeC(char *d) { *d = ; } void changeStr(NSString **s ...

  7. MATLAB入门学习(六)

    今天学三维作图 (*^__^*)…… 三维曲线作图 用到的命令:plot3 基本格式:plot3(x,y,z,s) 这里要画曲线,你需要知道该曲线的参数方程x=x(t),y=y(t),z=z(t) 然 ...

  8. bzoj3106 [cqoi2013]棋盘游戏

    Description 一个n*n(n>=2)棋盘上有黑白棋子各一枚.游戏者A和B轮流移动棋子,A先走. l         A的移动规则:只能移动白棋子.可以往上下左右四个方向之一移动一格. ...

  9. MySQL常用函数 一

    一.数学函数ABS(x)                    返回x的绝对值BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制)CEILING(x)                返 ...

  10. BZOJ4011:[HNOI2015]落忆枫音(DP,拓扑排序)

    Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出这样一个问题.  「相信吧.不然我们是什么,一团肉吗?要不是有灵魂……我们也 ...