Soalan:Large numbers are numbers that are significantly larger than those ordinarily used in everyday life. Large numbers often occur in fields such as cosmology. Computing large number using conventional 32-bit integer variable is problematic because large number is usually larger than 2
32 (4,294,967,296). One of the solutions is to compute large number using string of characters. This problem requires your program to ADD to large positive integers.
Input
The input will contain TWO string of characters, each represents a large number. You may assume that the numbers to be entered would not exceed 15 digits.
Output
The output will contain EITHER a string of characters representing the sum of the two large numbers entered OR, an error message if any of the numbers entered is not a large number.
Sample Input
11111111111 22222222222
11111111111 5555555555
5000000000 5000000000
100 999
Sample Output
33333333333
16666666666
10000000000
Error: One (or both) of the numbers given is not a large integer
Sumber: http://metalab.uniten.edu.my/~multiprog10/ Penerangan:Korang perlu menambah 2 buah nombor besar.
Analisis:Tidak ada apa yang perlu di fikirkan. Dengan mudah, aku menggunakan Class BigInteger yang terdapat dalam
java.math.*. Untuk maklumat lanjut mengenai Class BigInteger, korang boleh rujuk pada artikel berikut:
http://secure.javamalaysia.com/tutorial-matematik/menguruskan-nombor-integer-yang-sangat-besar/Kod Sumber:import java.math.BigInteger;
class LargeNumbers{
public static void main(String[] args){
BigInteger x = new BigInteger("100");
BigInteger y = new BigInteger("999");
BigInteger limit = new BigInteger("4294967296");
switch(x.compareTo(limit)){
case -1:
case 0:
System.out.println("Error: One (or both) of the numbers given is not a large integer");
break;
case 1:
switch(y.compareTo(limit)){
case -1:
case 0:
System.out.println("Error: One (or both) of the numbers given is not a large integer");
break;
case 1:
System.out.println(x.add(y));
break;
}
break;
}
}
}
ps: dalam masa terdekat, insya-Allah aku akan update thread ini untuk menunjukkan cara menyelesaikan masalah ini tanpa menggunakan sebarang API.