A + B Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 197537 Accepted Submission(s): 59076
Problem Description
Calculate A + B.
Input
Each line will contain two integers A and B. Process to end of file.
Output
For each case, output A + B in one line.
Sample Input
1 1
Sample Output
2
Author
HDOJ
分析:这个题目我是这样想的,模拟人的做法,用到了栈的。解答的思路在代码中有标示
(JAVA 中有超大数的类可以实现)
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String num1, num2;
BigDecimal big1, big2;
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
num1 = in.next();
num2 = in.next();
big1 = new BigDecimal(num1);
big2 = new BigDecimal(num2);
System.out.println(big1.add(big2));
}
}
}
先留着
package endual.answer;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String a, b;
a = cin.next();
b = cin.next();
char f1 = a.charAt(0) ; //保存a的符号
char f2 = b.charAt(0) ; //保存b的符号
int fSum = 0 ;
if (f1 == '-') {
a = a.substring(1) ;
fSum++ ;
}
if (f2 == '-') {
b = b.substring(1) ;
fSum++ ;
}
int aLen = a.length() ;
int bLen = b.length() ;
if (aLen < bLen) { //保证a的长度要大于b的长度
String tempBtoA = a ;
a = b ;
b = tempBtoA ;
char tempF = f1 ; //同样交换他们的符号
f1 = f2 ;
f2 = tempF ;
} //保证a的长度要大于等于b的长度
//如果是同号的
if(fSum != 1) {
String res = jia(a,b) ;
if(fSum == 2) {
res = "-" + res ;
}
System.out.println(res) ;
return ;
}
//如果是不同号的
String tempa = a ;
String tempb = b ;
//如果是相等的 那么返回0
if(a.equals(b)) {
System.out.println(0) ;
return ;
}
String c ;
boolean isBigger = getWhoIsGiger(tempa,tempb) ; //返回a和b那个大了
System.out.println(isBigger) ;
if (!isBigger) { //b 大
c = jian(b,a) ;
if(f2 == '-') {
c = "-" + c ;
}
} else { //a大
c = jianab(a,b) ;
if(f1 == '-') {
c = "-" + c ;
}
}
System.out.println(c) ;
} //end main
private static String jianab(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 + 10;
if (temp < 10) {
t = -1;
} else {
temp = temp - 10 ;
t = 0; // 将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) {
t = 0;
} else {
temp = temp - 10 ;
t = 1; // 将t设置成为1,此时将sumAB的1取得
}
sum.push(temp); // 添加到栈中去
}
String s = "" ;
while (!sum.isEmpty()) { // 如果两个里面其中有一个是空了,那么停止
s = s+sum.pop().toString();
}
return s ;
}
//不同号码相减
private static String jian(String a, String b) {
Stack stacka = new Stack();
Stack stackb = new Stack();
String ab = a;
System.out.println("-----a------ |-" +ab);
while (ab.length() != 0) {
char c = ab.charAt(0);
String subab = ab.substring(1);
stacka.push(c) ;
ab = subab ;
}
String abc = b;
System.out.println("-----b------|-" +abc);
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 + 10;
if (temp < 10) {
t = -1;
} else {
temp = temp - 10 ;
t = 0; // 将t设置成为1,此时将sumAB的1取得
}
sum.push(temp); // 添加到栈中去
}// end while ;
String s = "" ;
while (!sum.isEmpty()) { // 如果两个里面其中有一个是空了,那么停止
s = s+sum.pop().toString();
}
return s ;
}
//同号相加
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 ;
}
private static boolean getWhoIsGiger(String a, String b) {
if (a.length() > b.length()) {
return true ;
}
//a的长度等于b的长度
boolean sig = true ;
boolean isBiger = true ; // 默认a > b
while (b.length() != 0) { //因为b的长度小,所以它先没有
int intB = Integer.parseInt(b.charAt(0)+"") ;
b = b.substring(1) ;
int intA = Integer.parseInt(a.charAt(0)+"") ;
a = a.substring(1) ;
if(sig) {
if(intA < intB) { //高位上比较 b要大于a了
isBiger = false ;
sig = false ;
}
}
}
return isBiger;
}
} //end class
网上的代码是要计算a 和 b的长度,我在想,要是a和b的长度超过了int类型的长度,那么不是不能用了。所以用栈,这么就不用考虑a 和 b的长度了
代码还有问题的
哎 这个ACM还是有的难的