(hdu step 2.3.1)A + B Problem II(大数加法)

题目:

A + B Problem II

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

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


Author

Ignatius.L

题目分析:

简单题。这种题比赛的时候直接用java写就好,用C/C++来做其实也是那么个意思。java的BigInteger的相关类底层就是那么实现的。所以没必要写那么1、200,行代码。无论是开发也好,还是算法也好,个人觉得都应该选择最合适的方法来解决问题,而不要拘泥于某种技术。当然,C/C++关于大数的思想还是得看一下的,对于初学者来说,理解后自己敲一遍也是有必要的。但是过了初学阶段的话,个人觉得直接用java来做就好了。

代码如下:

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		int n = scanner.nextInt();
		int i;
		for(i = 1 ; i <= n ; ++i){
			System.out.println("Case "+ i +":");

			BigInteger a = scanner.nextBigInteger();
			BigInteger b = scanner.nextBigInteger();

			System.out.println(a+" + " + b + " = " + a.add(b));
			if(i != n){//最后一个不要出现空行,否则会PE
				System.out.println();
			}
		}
	}
}
时间: 2024-10-29 00:39:35

(hdu step 2.3.1)A + B Problem II(大数加法)的相关文章

A + B Problem II(大数加法)

一直格式错误,不想改了,没A 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 using namespace std; 6 7 int main() 8 { 9 int T,K=0; 10 scanf("%d",&T); 11 char a[1002],b[1002]; 12 int ta[1002],

HDU 1250 Hat&#39;s Fibonacci (递推、大数加法、string)

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14776    Accepted Submission(s): 4923   Problem Description A Fibonacci sequence is calculated by adding the previous two members

hdu1002 A + B Problem II(大数题)

题目链接: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 sim

HDU1002 A + B Problem II 大数问题

题目链接: 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): 409136    Accepted Submission(s): 79277 Problem Description I have a very si

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)Total Submission(s): 315214    Accepted Submission(s): 61139 Problem Description I have a very simple problem for you. Given two integers A and B, you

(hdu step 4.3.2)Prime Ring Problem(n个数成环,输出两两之和为质数的所有情况)

题目: Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 467 Accepted Submission(s): 297   Problem Description A ring is compose of n circles as shown in diagram. Put natural number

HDU 1023 Train Problem II 大数打表Catalan数

一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n,n) / (n+1); 递推公式 C(n ) = C(n-1)*(4*n-2) / (n+1) 高精度乘以一个整数和高精度除以一个整数的知识.这样还是使用整数数组比較好计算,假设使用string那么就不太好计算了,由于整数也可能是多位的. const int MAX_N = 101; short

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

(hdu step 1.3.1)FatMouse&#39; Trade(在收入需要一定的付出的情况下求最大收入)

题目: FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5092 Accepted Submission(s): 1530   Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gua