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 高精度,板子题 /************************************************************************* & ...
随机推荐
- MFC加速键
添加的函数: } // 解释说明 MFC对话框不自动处理加速键,你必须自己编写代码来做这件事情.为了理解弄清楚这是为什么,让我们回首Windows开发的历程,在使用C和原始的Windows ...
- iOS- 移动端Socket UDP协议广播机制的实现
1.前言 什么是UDP协议广播机制? 举一个例, 例如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):“谁是张三” 如果它是张三,它就会回应你,在网络中也是一样的. ...
- 原生javascript自定义input[type=checkbox]效果
2018年6月27日 更新 能用css3,就不用js 用纯css3实现样式重写 <!DOCTYPE html> <html lang="en"> < ...
- navicat for mysql 10.1.7 注册码
NAVN-LNXG-XHHX-5NOO名:组织:注册码:均为NAVN-LNXG-XHHX-5NOO 下载地址:http://www.cr173.com/soft/38153.html
- 在c++中调用exe程序进行操作
#include <Windows.h> #include <iostream> #include <direct.h> #define picNum 228 us ...
- (一)MySQL学习笔记
1.MySQL安装 下载地址:https://dev.mysql.com/downloads/mysql/ 启动mysql服务 net start mysql 停止mysql服务 net stop m ...
- 【转】c# 类反射简单操作
转:http://www.jb51.net/article/25863.htm 首先建立一个测试的类 复制代码代码如下: public class MyClass { public int one ...
- POJ1269:Intersecting Lines——题解
http://poj.org/problem?id=1269 题目大意:给四个点,求前两个点所构成的直线和后两个点所构成的直线的位置关系(平行,重合,相交),如果是相交,输出交点坐标. ——————— ...
- BZOJ4945 & 洛谷3825 & UOJ317:[NOI2017]游戏——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4945 https://www.luogu.org/problemnew/show/P3825 ht ...
- BZOJ4539 [Hnoi2016]树 【倍增 + 主席树】
题目链接 BZOJ4539 题解 我们把每次复制出来的树看做一个点,那么大树实际上也就是一棵\(O(M)\)个点的树 所以我们只需求两遍树上距离: 大树上求距离,进入同一个点后在模板树上再求一次距离 ...