-
[백준] 1247번 : 부호 [JAVA]Algorithm/백준[JAVA] 2024. 5. 5. 19:53
https://www.acmicpc.net/problem/1247
# 문제
# 접근방식
- 무한에 가까운 수를 연산할 때는 BigInteger를 사용해야 한다.
- 한 케이스마다 입력되는 수를 더해서 0이면 "0", 음수면 "-", 양수면 "+"를 출력하면 된다.
# 소스코드
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); for(int i=0; i<3; i++){ int n = Integer.parseInt(br.readLine()); BigInteger s = new BigInteger("0"); for(int j=0; j<n; j++){ BigInteger n2 = new BigInteger(br.readLine()); s = s.add(n2); } if(s.compareTo(BigInteger.ZERO) == -1) { System.out.println("-"); } else if(s.compareTo(BigInteger.ZERO) == 1) { System.out.println("+"); } else { System.out.println("0"); } } } }
# 성능
# 회고
처음에는 s도 int로 했다.
너무 쉬워서 아닐거라고 예상은 했지만,, 역시나,, 런타임 에러가 나버렸다.
찾아보니 무한에 가까운 큰 수에는 BigInteger를 써야한다는걸 알았다.
BigInteger와 관련된 정보는 아래 포스팅을 통해 확인할 수 있다.
'Algorithm > 백준[JAVA]' 카테고리의 다른 글
[백준] 1032번 : 명령 프롬프트 [JAVA] (0) 2024.05.06 [백준] 1267번 : 핸드폰 요금 [JAVA] (0) 2024.05.05 [백준] 1085번 : 직사각형에서 탈출 [JAVA] (0) 2024.05.05 [백준] 2530번 : 인공지능 시계 [JAVA] (0) 2024.05.05 [백준] 2083번 : 럭비 클럽 [JAVA] (0) 2024.05.05