HDU-1002,A + B Problem II(Java大数)

简介: HDU-1002,A + B Problem II(Java大数)

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


AC Code:  


import java.util.*;
import java.math.*;
public class Main
{
  public static void main(String[] args)
  {
    Scanner input=new Scanner(System.in);
    int t=input.nextInt();
    int ans=1;
    for(int i=1;i<=t;i++)
    {
      BigDecimal a=input.nextBigDecimal();
      BigDecimal b=input.nextBigDecimal();
      System.out.println("Case "+ans+":");
      System.out.println(a+" + "+b+" = "+a.add(b));
      ans++;
      if(ans<=t)
        System.out.println();
    }
    input.close();
  }
}


相关文章
|
6月前
|
Java 编译器
有关电脑中idea编译报错问题java: No implementation was created for AdminUserConverter due to having a problem in
有关电脑中idea编译报错问题java: No implementation was created for AdminUserConverter due to having a problem in
314 0
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
|
Cloud Native Java Go
解决 Spring Boot 和 Gradle Java 版本兼容性问题:A problem occurred configuring root project ‘demo1‘. > Could n
解决 Spring Boot 和 Gradle Java 版本兼容性问题:A problem occurred configuring root project ‘demo1‘. > Could n
889 0
|
Java
Java 中大数的处理方案BigInteger和BigDecimal类的使用
Java 中大数的处理方案BigInteger和BigDecimal类的使用
90 0
|
安全 Java API
Java的第十篇文章——常用类下(String、大数类、日期类和包装类)
Java的第十篇文章——常用类下(String、大数类、日期类和包装类)
|
Java 编译器
有关电脑中idea编译报错问题java: No implementation was created for AdminUserConverter due to having a problem in
有关电脑中idea编译报错问题java: No implementation was created for AdminUserConverter due to having a problem in
1375 0
有关电脑中idea编译报错问题java: No implementation was created for AdminUserConverter due to having a problem in
|
Java 索引
java中大数的计算BigInteger和BigDecimal两个类的常用方法
java中大数的计算BigInteger和BigDecimal两个类的常用方法
98 0
|
Java
java 大数处理方案指南
1.Java大数值处理方案 如果基本的整数和浮点数精度不能够满足需求,那么可以使用java.math包中的两个很有用的类:BigInteger和BigDecimal。这两个类可以处理包含任意长度数字序列的数值。BigInteger类实现了任意精度的整数运算,BigDecimal实现了任意精度的浮点数运算 使用静态的valueOf方法可以将普通的数值转换为大数值:
221 0
|
存储 Java
大数计算题—Java选手的做法
大数计算题—Java选手的做法
124 0
大数计算题—Java选手的做法
|
Java
poj3320Jessica's Reading Problem—尺取法(java)
这个和裸的尺取优点不同的是,他需要一个map来维护判断而不是sum维护判断。在右侧从左向右遍历的同时,用一个map<Integer,Integer>来维护元素,map.keyset()就可以判断是否包含所有元素,数值用来判断改元素出现次数是否应移除改元素—左侧标记右移的时候判断。
86 0