Username:

Password:

Pages: [1]
  Print  
Author Topic: MultiProg'10: Large Number  (Read 85 times)
j33h4d
ngah busy exam sampai 24 Mac 10 =|
Administrator
Freshie
*
Posts: 22



View Profile WWW Email
« on: March 05, 2010, 10:48:26 pm »

Quote
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 232 (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.
Quote
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:

Code:
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.
« Last Edit: March 05, 2010, 10:52:31 pm by j33h4d » Logged

"programming is all about the arrangement of codes" ~ Fikri Fadzil
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF | SMF © 2006-2009, Simple Machines LLC
Installed by Installatron

DarkBreak by DzinerStudio