코딩테스트/백준
[Dev-Ping9] 백준 13305번 - 주유소 100점 답안 (Java) (서브태스크)
DevPing9_
2022. 1. 10. 00:30


# 문제 설명
처음 풀어본 서브 태스크 문제이다.
서브태스크 1, 2번은 로직을 짤 수 있는가에 대한 문제이다.
서브태스크 3번은 큰수의 덧셈 곱셈을 구현할 줄 아는가에 대한 문제이다.
(또 구현하기 귀찮아서 찾아보니 자바에는 라이브러리가 있었다. 왜... 직접 구현한거지 나는...)

# 코드 구현 (58점 , 100점 두개의 코드가 있습니다)
* 항상 예외 케이스를 체크하자 (n=2 일때는 1번 코드도 0점짜리이다. 다행히 58점까지의 테스트케이스에 n=2 일때가 없는듯)
1. 로직만 구현 (58점)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class test{
static int[] road;
static int[] oilPrice;
static int[] checkPoint;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
road= new int[n-1];
oilPrice = new int[n];
checkPoint = new int[n];
for(int i=0; i<n-1; i++){
road[i]=Integer.parseInt(st.nextToken());
}
st= new StringTokenizer(br.readLine(), " ");
int min = 9999; int idx =0;
for(int i=0; i<n; i++){
oilPrice[i]=Integer.parseInt(st.nextToken());
if(min>oilPrice[i]){
min=oilPrice[i];
checkPoint[idx++]=i;
}
}
checkPoint[idx]= road.length;
int totalPrice = 0;
for(int i=0; i<idx; i++){
for(int j=checkPoint[i]; j<checkPoint[i+1]; j++){
totalPrice+= road[j]*oilPrice[checkPoint[i]];
}
}
System.out.println(totalPrice);
}
}
2. 큰수 다루기 (100점)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class Main{
static BigInteger[] road;
static BigInteger[] oilPrice;
static int[] checkPoint;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
road= new BigInteger[n-1];
oilPrice = new BigInteger[n];
checkPoint = new int[n+1];
StringTokenizer st;
if(n>2){
st = new StringTokenizer(br.readLine(), " ");
for(int i=0; i<n-1; i++){
road[i]=new BigInteger(st.nextToken());
}
}else{
road[0]=new BigInteger(br.readLine());
}
st= new StringTokenizer(br.readLine(), " ");
BigInteger min=BigInteger.valueOf(0L);
int idx =0;
for(int i=0; i<n; i++){
oilPrice[i]=new BigInteger(st.nextToken());
if(i==0 || min.compareTo(oilPrice[i])==1){
min=oilPrice[i];
checkPoint[idx++]=i;
}
}
checkPoint[idx]= road.length;
BigInteger totalPrice = BigInteger.valueOf(0);
for(int i=0; i<idx; i++){
for(int j=checkPoint[i]; j<checkPoint[i+1]; j++){
totalPrice = totalPrice.add(
road[j]
.multiply(
oilPrice[checkPoint[i]]
)
);
}
}
System.out.println(totalPrice);
}
}
/*
10
5 5 5 5 5 5 5 5 5
2 50 10 30 20 1 3 3 3 3
-> 5*(5*2) = 50
-> 5*(1*4) = 20
*/
/*
2
5000000000000000000000000000000000000
5000000000000000000000000000000000000000000000000000000000000000000000 50000000000000000000000000000000000
*/
728x90