欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ACM 1002

程序员文章站 2022-05-22 08:32:52
...

A + B Problem II

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


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

 

 

  转的答案 ac了

 

 import java.math.BigDecimal;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		int t, i;
		String num1, num2;
		BigDecimal big1, big2;
		Scanner in = new Scanner(System.in);
		t = in.nextInt();
		for (i = 0; i < t; i++) {
			num1 = in.next();
			num2 = in.next();
			big1 = new BigDecimal(num1);
			big2 = new BigDecimal(num2);
			System.out.println("Case " + (i + 1) + ":");
			System.out.println(big1 + " + " + big2 + " = " + big1.add(big2));
			if (i != t - 1)
				System.out.println();
		}
	}
}

 

 

我的答案 wrong 不知道是为什么

 

package endual;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {

	public static void main(String[] args) {

		Scanner cin = new Scanner(System.in);
		int n = 0;
		n = cin.nextInt();
		List<String> list = new ArrayList<String>();
		String a, b;

		for (int i = 0; i < n; i++) {

			a = cin.next();
			b = cin.next();
			
			String tepa = a; 
			String tepb = b;
			
			int aLen = a.length();
			int bLen = b.length();
			if (aLen < bLen) { // 保证a的长度要大于b的长度
				String tempBtoA = a;
				a = b;
				b = tempBtoA;
			} // 保证a的长度要大于等于b的长度
			String he = jia(a, b);
			list.add(tepa + " + " + tepb + " = " + he);
		} // end for
		System.out.println();
		for (int j = 0; j < list.size(); j++) {
			System.out.println("Case " + (j + 1) + ":");
			System.out.println(list.get(j));
			System.out.println();

		}

	}

	private static String jia(String a, String b) {

		Stack stacka = new Stack();
		Stack stackb = new Stack();
		String ab = a;
		while (ab.length() != 0) {

			char c = ab.charAt(0);
			String subab = ab.substring(1);
			stacka.push(c);
			ab = subab;
		}

		String abc = b;
		while (abc.length() != 0) {

			char c = abc.charAt(0);
			String subabc = abc.substring(1);
			stackb.push(c);
			abc = subabc;
		}

		Stack sum = new Stack();
		int temp = 0;
		int t = 0;
		while (!stackb.isEmpty()) { // 如果两个里面其中有一个是空了,那么停止

			int aInt = Integer.parseInt(stacka.pop().toString());
			int bInt = Integer.parseInt(stackb.pop().toString());
			temp = aInt + bInt + t;
			if (temp < 10) {
				t = 0;
			} else {
				temp = temp - 10;
				t = 1; // 将t设置成为1,此时将sumAB的1取得
			}
			sum.push(temp); // 添加到栈中去
		}// end while ;

		while (!stacka.isEmpty()) {

			int aInt = Integer.parseInt(stacka.pop().toString());
			temp = aInt + t;
			if (temp < 10) {
				sum.push(temp);
				t = 0;
			} else {

				temp = temp - 10;
				t = 1; // 将t设置成为1,此时将sumAB的1取得
			}
			sum.push(temp); // 添加到栈中去

		}
		if (t == 1) {

			sum.push(t);
		}
		String s = "";
		while (!sum.isEmpty()) { // 如果两个里面其中有一个是空了,那么停止

			s = s + sum.pop().toString();

		}
		return s;

	}

}