반응형

자바는 한번씩 느끼는건데 C / Cpp보다 편하게 이용할 수 있는 객체가 많이 제공되고 있는 것 같다.


이번에는 큰수를 다룰 때 자바에서는 어떤 객체를 이용하는지 알아보려한다.


Import


import java.math.BigInteger;



객체 생성


BigInteger 객체명 = new BigInteger("초기화할 수");


ex) BigInteger a = new BigInteger("0");



1. a에 20을 더하는 방법

a = a.add(BigInteger.valueOf(20));



2. a에 2를 빼는 방법

a = a.subtract(BigInteger.valueOf(2));



3. a에 2를 곱하는 방법

a = a.multiply(BigInteger.valueOf(2));



4. a에 3를 나누는 방법

a = a.divide(BigInteger.valueOf(3));



5. a를 5로 나눌 때 나머지를 구하는 방법

a = a.remainder(BigInteger.valueOf(5));




---

여담으로 알고리즘 문제 중에서 큰 수 A+B(https://www.acmicpc.net/problem/10757)라는 문제가 있는데


C를 이용하여 푼 코드는 http://www.crocus.co.kr/270 


위의 링크와 같지만 반면에


Java를 이용하여 푼 코드는 아래와 같다.(매우 간결하고 가독성이 좋다.)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.math.BigInteger;
import java.util.Scanner;
 
public class Main {
         
    public static void main(String[] args)
    {
        BigInteger a = new BigInteger("0");
        BigInteger b = new BigInteger("0");
 
        BigInteger sum = new BigInteger("0");
        
        Scanner sc = new Scanner(System.in);
 
        a = sc.nextBigInteger();
        b = sc.nextBigInteger();
        
        sum = sum.add(a);
        sum = sum.add(b);
        
        System.out.println(sum);
    }
    
}
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus






소스 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package JavaBasic;
import java.math.BigInteger;
import java.util.Scanner;
 
public class Jmain {
         
    public static void main(String[] args)
    {
        BigInteger a = new BigInteger("0");
        BigInteger b = new BigInteger("0");
 
        BigInteger sum = new BigInteger("0");
        
        Scanner sc = new Scanner(System.in);
 
        a = sc.nextBigInteger();
        b = sc.nextBigInteger();
        
        sum = sum.add(a);
        sum = sum.add(b);
 
        System.out.println(sum);
        
        System.out.println("-------------");
        a = BigInteger.valueOf(0);
        
        //1. a에 20을 더하는 방법
        a = a.add(BigInteger.valueOf(20));
        System.out.println("a += 20 :: " + a);
        
        //2. a에 2를 빼는 방법
        a = a.subtract(BigInteger.valueOf(2));
        System.out.println("a -= 2 :: " + a);
        
        
        //3. a에 2를 곱하는 방법
        a = a.multiply(BigInteger.valueOf(2));
        System.out.println("a *= 2 :: " + a);
        
        
        //4. a에 3를 나누는 방법
        a = a.divide(BigInteger.valueOf(3));
        System.out.println("a /= 3 :: " + a);
        
        
        //5. a를 5로 나눌 때 나머지를 구하는 방법
        a = a.remainder(BigInteger.valueOf(5));
        System.out.println("a %= 5 :: " + a);
    }
    
}
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형