题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 230247    Accepted Submission(s): 44185

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

题目大意:题意很容易理解,具体就不解释了,主要就是要解决大数的问题。

题目思路:如果会java的话,可以轻松AC。其他的小伙伴们只能用最笨的方法解决。我们用一个数字将数字倒过来存下,无论是乘法还是加法,这是最好的解决办法。

下面附上两个代码。

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int main ()
{
char a[],b[];
int na[],nb[],sum[],pre,flag=;
int t;
scanf("%d",&t);
while (t--)
{
memset(sum,,sizeof(sum));
memset(na,,sizeof(na));
memset(nb,,sizeof(nb));
scanf("%s%s",a,b);
pre=;
int lena=strlen(a);
int lenb=strlen(b);
for (int i=; i<lena; i++)
na[lena--i]=a[i]-'';
for (int j=; j<lenb; j++)
nb[lenb--j]=b[j]-'';
int lenx=lena>lenb?lena:lenb;
for (int k=; k<lenx; k++)
{
sum[k]=na[k]+nb[k]+pre/;
pre=sum[k];
}
while (pre>)
{
sum[lenx]=pre/%;
lenx++;
pre/=;
}
printf ("Case %d:\n",flag++);
printf ("%s + %s = ",a,b);
for (int i=lenx-; i>=; i--)
{
printf ("%d",sum[i]%);
}
printf ("\n");
if (t)
printf ("\n");
} return ;
}

java代码。

 import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int l=sc.nextInt();
for(int i=;i<=l;i++){
if(i!=) System.out.println();
BigInteger a,b;
a=sc.nextBigInteger();
b=sc.nextBigInteger();
System.out.println("Case "+i+":");
System.out.println(a+" + "+b+" = "+a.add(b));
}
}
}

hdu1002 A + B Problem II(大数题)的更多相关文章

  1. hdu1002 A + B Problem II[大数加法]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1002 题干 代码和解释 由题意这是一个涉及到大数的加法问题.去看了一眼大数加法的方法感觉头很大,然后突然发现Java可以流氓解决大数问题,毅 ...

  2. HDU1002 -A + B Problem II(大数a+b)

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

  3. HDU1002 A + B Problem II 大数问题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java ...

  4. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  5. hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏

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

  6. [HDU1002] A + B Problem II

    Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...

  7. A + B Problem II 大数加法

    题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...

  8. A + B Problem II(大数加法)

    一直格式错误,不想改了,没A #include <iostream> #include <stdio.h> #include <string.h> #include ...

  9. HDU 1023 Train Problem II 大数打表Catalan数

    一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n ...

随机推荐

  1. /proc/meminfo中meminfo的计算方法

    /proc/meminfo里的可使用内存的计算没有那么简单,并不是简单的free和page cache的加和 free + pagecache 以此为基准 但是需要减去一些内存:首先要减去系统预留的内 ...

  2. 请问:在delphi中怎样判断DBgrid中数据是否被修改,以便在退出窗口时加以提示

    若DBGrid.DataSource.DateSet为ADOQuery1,这样试一下:if ADOQuery1.Modified then ... procedure TForm1.FormClose ...

  3. Java线程模型

    并发不一定要依赖多线程(如PHP中很常见的多进程并发),但是在Java里面谈论并发,大多数都与线程脱不开关系. 线程是比进程更轻量级的调度执行单位,线程的引入,可以把一个进程的资源分配和执行调度分开, ...

  4. BZOJ 1149 风铃(树形DP)

    题目描述的实际是一颗二叉树,对于每个结点,要么满叉,要么无叉. 对于一种无解的简单情况,我们搜一遍树找到最浅的叶子结点1和最深的叶子结点2,如果dep[1]<dep[2]-1,则显然无解. 所以 ...

  5. 【zoj2314】Reactor Cooling 有上下界可行流

    题目描述 The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuc ...

  6. 【bzoj3524】[Poi2014]Couriers 主席树

    题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...

  7. P2587 [ZJOI2008]泡泡堂

    题目描述 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂.每一场比赛前,对阵双方的教练向组 ...

  8. 【题解】Atcoder AGC#16 E-Poor Turkeys

    %拜!颜神怒A此题,像我这样的渣渣只能看看题解度日╭(╯^╰)╮在这里把两种做法都记录一下吧~ 题解做法:可以考虑单独的一只鸡 u 能否存活.首先我们将 u 加入到集合S.然后我们按照时间倒序往回推, ...

  9. [洛谷P4139]上帝与集合的正确用法

    题目大意:多次询问,每次给你$p$询问$2^{2^{2^{\dots}}}\bmod p$ 题解:扩展欧拉定理,求出$\varphi(p)$即可.因为$2^{2^{2^{\dots}}}>> ...

  10. BZOJ1999 NOIP2007 洛谷P1099 P2491 SDOI 2011

    Description: 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W表示各 ...