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;
}
}
分享到:
相关推荐
浙江大学acm题1002道的答案 个人答案 仅供参考
性能问题并没有解决,看了高手们的代码,实在是不想复制算法了,就写在这里吧,万恶的性能问题啊!
大数相加 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) ...
杭电acm1002源码,帮助不会做的Acmer
pku acm 1002 487-3279代码 二叉查找数实现 解题报告请访问:http://blog.csdn.net/china8848
该文件包含了杭电ACM第1002至1025题得个人解题答案,有些答案包含了两种至三种解答。
本人在ACM上做题时遇到不少问题,在网上寻求不少答案后终于弄明白。答案已被杭电AC,希望对各位有帮助。。
C语言,进行大数据加减,当超过量程时候用数组~
看看的思路。。。。。。。。。。。。。。 我有点错,帮帮我看看哦
用C++自己编的,感觉比较简单,定义两个字符数组存储两个大数,再定义一个正西数组存放结果
北大ACM···················北大ACM··················
这是用java写的一些关于北大acm的程序,有一定的问题,但是绝大部分有用
北大ACM题库[1002-3200].iso 北大ACM题库[1002-3200].iso
ACM杭电Problem 1002 C++程序 大数相加问题,注意输出的限制
HDU_ACM_1002_大数相加C源代码,利用字符串处理
这是一些杭电ACM题目文档 刚AC的 1002 1003 1005 1008 1021 2153 1042 1161 1106 1061 1019 1013 1201 1097
Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University ...
北大 acm 程序源代码 1001 1002 …… 1008
从1002到3422的全部题目,很方便,不用上网也可练习北大acm的题目。