题目链接: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. PHPcms企业黄页中,会员注册之后提示经营模式不得少于1个字符 的解决办法

    后台--模块- 黄页模块 ---  企业库 --- 字段 ---   经营模式 ---  字符长度取值范围 1  改为  0. 

  2. 全面了解 Nginx 到底能做什么

    来源:https://www.jianshu.com/p/8bf73d1a758c 前言 本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可 ...

  3. HashMap源码剖析及实现原理分析(学习笔记)

    一.需求 最近开发中,总是需要使用HashMap,而为了更好的开发以及理解HashMap:因此特定重新去看HashMap的源码并写下学习笔记,以便以后查阅. 二.HashMap的学习理解 1.我们首先 ...

  4. 【刷题】BZOJ 3510 首都

    Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从 ...

  5. [洛谷P4118][Ynoi2016]炸脖龙I([洛谷P3934]Nephren Ruq Insania)

    题目大意:有$n$个数,每个数为$s_i$,两个操作: $1\;l\;r\;x:$表示将区间$[l,r]$内的数加上$x$ $2\;l\;r\;p:$表示求$s_l^{s_{l+1}^{^{s_{l+ ...

  6. [洛谷P2774]方格取数问题

    题目大意:给你一个$n\times m$的方格,要求你从中选择一些数,其中没有相邻两个数,使得最后和最大 题解:网络流,最小割,发现相邻的两个点不可以同时选择,进行黑白染色,原点向黑点连一条容量为点权 ...

  7. BZOJ1013:[JSOI2008]球形空间产生器——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1013 Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在 ...

  8. PE格式示意图

  9. [Leetcode] Maximum depth of binary tree二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  10. BZOJ2186 [Sdoi2008]沙拉公主的困惑 【数论,欧拉函数,线性筛,乘法逆元】

    2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 5003  Solved: 1725 [Submit] ...