hdu1002 A + B Problem II(大数题)

时间:2023-02-13 12:16:44

题目链接: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 &plus; B Problem II&lbrack;大数加法&rsqb;

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

  2. HDU1002 -A &plus; B Problem II&lpar;大数a&plus;b&rpar;

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

  3. HDU1002 A &plus; 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 &plus; B Problem II 大数相加 -提交通过

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

  5. hdu1002 A &plus; B Problem II&lpar;高精度加法&rpar; 2016-05-19 12&colon;00 106人阅读 评论&lpar;0&rpar; 收藏

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

  6. &lbrack;HDU1002&rsqb; A &plus; 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 &plus; B Problem II 大数加法

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

  8. A &plus; B Problem II&lpar;大数加法)

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

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

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

随机推荐

  1. 【bzoj2654】 tree

    http://www.lydsy.com/JudgeOnline/problem.php?id=2654 (题目链接) 题意 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有nee ...

  2. Javabean&plus;servlet&plus;JSP&lpar;html&rpar;实例应用

    大家都知道Javabean+servlet+JSP是最简单的MVC模式.的确,在一个小型的项目中,这个模式完全够用. 它优雅并且简洁.加上jQueryui的完美展示效果,让这个模式看起来非常合适.当然 ...

  3. 第33讲:List的一阶函数操作代码实战详解

    今天来看一下关于List的一阶函数操作 让我们看下下面的代码 println(List(1,2,3,4):::List(4,5,6,7,8):::List(10,11))//列表连接    print ...

  4. MySQL ALTER语法的运用方法 &amp&semi;&amp&semi; 操作索引和字段

    语法:alter_specification: ADD [COLUMN] create_definition [FIRST | AFTER column_name ] or ADD INDEX [in ...

  5. ubutun中安装nginx

    一.安装 sudo wget http://nginx.org/download/nginx-1.4.4.tar.gz sudo tar zxvf ng....cd nginx-1.4.4sudo . ...

  6. Maven学习:常用mvn命令

    转自:http://blog.csdn.net/lfsfxy9/article/details/12200915 Maven 在线: <span style="font-family: ...

  7. html 页面视图中的资源文件(css&sol;js&sol;image)的路径问题。

    说到html 页面视图中的资源文件的路径引用问题,这个问题以前一直没去弄明白.今天,我将公司新开发的一个项目完全移植到我本地搭建的php 环境中来,遇到了这个问题,想了一下,然后也不是很困难的就把它给 ...

  8. python 、mmap 实现内存数据共享

    import mmap mmap_file = None ##从内存中读取信息, def read_mmap_info(): global mmap_file mmap_file.seek(0) ## ...

  9. Android Studio NDK开发环境搭建

    一.   下载安装Android studio 和 NDK 二.   在Android studio中配置NDK(和SDK配置一样) 三.   用Android studio建立一个工程,打开proj ...

  10. C&num;集合类型大揭秘

    集合是.NET FCL(Framework Class Library)的重要组成部分,我们平常撸C#代码时免不了和集合打交道,FCL提供了丰富易用的集合类型,给我们撸码提供了极大的便利.正是因为这种 ...