알고리즘/백준알고리즘

[백준알고리즘] #2338. 긴자리 계산 (by JAVA and node.js)

Jimnya 2022. 4. 12. 09:41
728x90
반응형

백준알고리즘

- Bronze 5 -

 #2338. 긴자리 계산 by JAVA and node.js 

 


 

문제

출처: https://www.acmicpc.net/problem/2338

 

1271번 엄청난 부자의 문제처럼 BigInteger를 사용하는 문제


KeyPoint: BigInteger
 - int type: -2,147,483,648 ~ +2,147,483,647
 - BigInteger : 문자열 형태로, 범위는 무한대
 - 계산법(in JAVA): 
   > + : add()
   > - : subtract()
   > x : multiply()
   > % : divide()

풀이

▶ JAVA

import java.util.*;
import java.math.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        BigInteger a = scan.nextBigInteger();
        BigInteger b = scan.nextBigInteger();
        scan.close();
        System.out.println(a.add(b));
        System.out.println(a.subtract(b));
        System.out.println(a.multiply(b));
    }
}

▶ node.js

var fs = require('fs');
var input = fs.readFileSync('/dev/stdin').toString().split('\n').map(BigInt);
var a = input[0];
var b = input[1];
console.log((a+b).toString());
console.log((a-b).toString());
console.log((a*b).toString());

첫째 줄, 둘째 줄로 입력값이 들어오기 때문에 split('\n') 으로 해줘야 한다.


 

결과

 


 

 

 

 

 

End.

heisely's 괴발개발 개발일지

 

728x90
반응형