백준 1247

hong030·2023년 1월 30일
0
  • solved.ac 기준 브론즈 4단계 문제

풀이)
입력 단위가 크기 때문에 BigInteger 패키지를 활용한다.
시간을 줄이기 위해 BufferedReader을 활용한다.

BigInteger sum = new BigInteger("0"); // sum을 위해 초기값 0을 가지는 변수 설정
sum = sum.add(input); // BigInteger 객체는 단순 연산으로 계산이 안되고 클래스 내부 메소드를 활용해야 한다.
sum.compareTo(BigInteger.ZERO) == 1 // 양수
sum.compareTo(BigInteger.ZERO) == -1 // 음수
sum.compareTo(BigInteger.ZERO) == 0 // 0

내 코드)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import java.math.BigInteger;

public class Main{
	public static void main(String[]args) throws IOException{
		
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String str[] = new String[3];
		
		for(int k = 0;k<3;k++) {
			int num = Integer.parseInt(bf.readLine());
			BigInteger sum = new BigInteger("0");
			
			for(int i = 0;i<num;i++) {
				BigInteger input = new BigInteger(bf.readLine());
				sum = sum.add(input);
			}
	
			if(sum.compareTo(BigInteger.ZERO) == -1)
				str[k] = "-";
			else if (sum.compareTo(BigInteger.ZERO) == 1)
				str[k] = "+";
			else 
				str[k] = "0";
			
		}
		for(int k = 0;k<3;k++) {
			System.out.println(str[k]);
		}
		
	}	
}
profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글