题目链接: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. C# Designer.cs

    designer.cs 是窗体设计器生成的代码文件,作用是对窗体上的控件做初始化工作(在函数InitializeComponent()中) VS2003以前都把这部分代码放到窗体的cs文件中,由于这部 ...

  2. 【问题解决】Project facet Java version 1.7 (或者1.8)is not supported.

    在移植eclipse项目时,如果遇到 “Project facet Java version 1.7 is not supported.” 项目中的jdk1.7不支持.说明项目是其他版本jdk编译的, ...

  3. Tomcat 7优化配置

    Tomcat 的优化不像其它软件那样,简简单单的修改几个参数就可以了,它的优化主要有三方面,分为系统优化,Tomcat 本身的优化,Java 虚拟机(JVM)调优.系统优化就不在介绍了,接下来就详细的 ...

  4. 通过logger命令记录日志

    通过logger命令记录日志 logger是一个shell命令接口,可以通过该接口使用Syslog的系统日志模块,还可以从命令行直接向系统日志文件写入一行信息. ------------------- ...

  5. BZOJ1023:[SHOI2008]仙人掌图——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1023 Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple ...

  6. BZOJ2724:[Violet 6]蒲公英——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2724 输入格式 第一行两个整数n,m,表示有n株蒲公英,m次询问. 接下来一行 n 个空格分隔的整数 ...

  7. CF633C:Spy Syndrome 2——题解

    https://vjudge.net/problem/CodeForces-633C http://codeforces.com/problemset/problem/633/C 点击这里看巨佬题解 ...

  8. POI 2018.10.27

    [POI2015]LOG 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进 ...

  9. 清北学堂模拟赛d6t6 棋盘迷宫

    3.棋盘迷宫(boardgame.pas/c/cpp)(boardgame.in/out)时间限制:5s/空间限制:256M[题目描述]小 A 和小 Z 是非常要好的朋友, 而且他们都对迷宫游戏非常有 ...

  10. Java反转字符串的方式?

    1. 将String转换成字符数组,再利用字符数组进行首尾调换. 2. 利用递归的方式,主要是:reverse(str.substring(1)) + str.charAt(0); 3. 虽然Stri ...