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 高精度,板子题 /************************************************************************* & ...
随机推荐
- MDK中的调试脚本分析
准备写一个简单的裸机程序放mini2440里面跑,看到芯片手册有2中启动方式,1.从Nor Flash启动 2.从Nand Flash中启动.断断续续弄了几天始终无法烧录进去,想起平时自己工作中调试程 ...
- xpath获取块元素下<br>下的信息
再爬虫取字段的时候遇到一种类似下面的结构: <p> <br> "通用名称:xxxxxx" </p> 用xpath取的方式://p//text() ...
- Windows API封装:LoadLibrary/FreeLibrary
LoadLibrary/LoadLibraryEx用来加载DLL到自己的进程空间,使用完用FreeLibrary释放,一般使用方式如下: HINSTANCE hInstRich = ::Load ...
- centos7编译安装redis遇坑
编译redis时:make cc Command not found 原因分析:没有安装gcc,执行: yum install gcc 编译redis时:error: jemalloc/jemallo ...
- [剑指Offer] 58.对称的二叉树
题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. [思路]递归,关键是isSame函数中的最后一句 /* struct Tree ...
- ismember matlab
ismember 判断A中的元素在B中有没有出现 LIA = ismember(A,B) for arrays A and B returns an array of the same size as ...
- 【bzoj1572】[Usaco2009 Open]工作安排Job 贪心+堆
题目描述 Farmer John 有太多的工作要做啊!!!!!!!!为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有1000000000个单位时间(!). ...
- HTML5 应用程序缓存
使用HTML5,通过创建 cache manifest 文件,可以轻松创建web应用的离线缓存. 什么事应用程序缓存? HTML5引入了应用程序缓存,这意味着 web 应用可进行缓存,并在没有因特 ...
- react 入门与进阶教程
react 入门与进阶教程 前端学习对于我们来说越来越不友好,特别是随着这几年的发展,入门门槛越来越高,连进阶道路都变成了一场马拉松.在学习过程中,我们面临很多选择,vue与react便是一个两难的选 ...
- [LouguT30212]玩游戏
题面在这里 description 对于\(k=1,2,...,t\),求\[\frac{1}{nm}\sum_{i=1}^{n}\sum_{j=1}^{m}(a_i+b_j)^k\] 对\(9982 ...