HDU 1002 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
分析:
大数问题肯定是当作字符串来处理的,首先要把数的每个位数逆序存到一个int型的数组中,然后将这两个数组的对应位置上的数相加,如果有进位的话还应加上进位,最后输出的时候应该先找到首个不为0 的数,然后将后面的数输出来。
代码:
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
getchar();
for(int h=1; h<=n; h++)
{
char a[10000],b[10000];
int c[10000],d[10000];
int e[10000];
int k,l;
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
scanf(" %s %s",a,b);
k=strlen(a);
l=strlen(b);
for(int i=0; i<k; i++)
c[i]=a[k-i-1]-'0';
for(int i=0; i<l; i++)
d[i]=b[l-i-1]-'0';
int op=0;
for(int i=0; i<1000; i++)
{
e[i]=(c[i])+(d[i])+op;
if(e[i]>=10)
{
e[i]=e[i]%10;
op=1;
}
else op=0;
}
printf("Case %d:\n",h);
printf("%s + %s = ",a,b);
int i;
for( i=999; i>=0; i--)
if(e[i]!=0)break;
for(int j=i; j>=0; j--)
printf("%d",e[j]);
printf("\n");
if(h!=n)
printf("\n");
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
}
return 0;
}
HDU 1002 A + B Problem II (大数加法)的更多相关文章
- HDU 1002 A + B Problem II(高精度加法(C++/Java))
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...
- 大数加法~HDU 1002 A + B Problem II
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...
- hdu 1002 A + B Problem II【大数加法】
题目链接>>>>>> 题目大意:手动模拟大数加法,从而进行两个大数的加法运算 #include <stdio.h> #include <strin ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- HDU 1002 A + B Problem II(大整数相加)
A + B Problem II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- A + B Problem II 大数加法
题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...
- hdu 1002 A + B Problem II(大数)
题意:就是求a+b (a,b都不超过1000位) 思路:用数组存储 第一道大数的题目,虽然很水,纪念一下! 代码: #include<cstdio> #include<cstring ...
- HDU 1002 A + B Problem II( 高精度加法水 )
链接:传送门 题意:A + B 高精度,板子题 /************************************************************************* & ...
随机推荐
- VS2005、VS2008中的快捷键、组合键大全
Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...
- 发生dev_queue_xmit的时候,全部都是从ip_finish_output中来的吗
也就是说啊,内核中的收发包的路径,很可能是经理driver_recv --> tcp -->driver_send这样一个过程,是个很长的路径呢...... 从dev_queue_xmit ...
- Java模块化开发
包配置, 静态资源, 视图解析器, 数据库,
- jstack分析线程死锁
一.介绍 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...
- canvas drawImage 不显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Bootstrap 字体图标、下拉菜单、按钮组
Bootstrap 字体图标(Glyphicons) 需要引入fonts文件夹中的文件,而且该文件夹必须命名为fonts,然后引进css文件,jQuery文件,以及bootstrap的js文件. 用法 ...
- [NOIP2016]愤怒的小鸟 状态压缩dp
题目描述 Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可以用它向第一象限发射一只红色的小鸟,小鸟们的飞行轨迹均为形 ...
- 【原创】宿主机远程登录虚拟机(windows server 2003系统)
转载请注明,谢谢合作 1.虚拟机网络设置为 “桥接模式”如图 2.系统装好并登陆后 右键点击我的电脑,点击属性,然后在弹出来的选择框中勾选远程桌面-->启用这台计算机的远程桌面 然后点添加-- ...
- BZOJ2693:JZPTAP——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2693 Description Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 ...
- excel 列索引(数字)转列名
function index2ColName($columnNumber) { $dividend = $columnNumber; while ($dividend > 0) { $modul ...