ACM 1000
8433 ワード
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はやはりあります难しいです