https://www.acmicpc.net/problem/24885
24885번: 주식
첫째 줄에 과거에 머무를 수 있는 기간 $N(2\le N \le 10)$, 숭고한이 가지고 간 돈 $M(0 \le M \le 1\,000)$, 대출할 수 있는 한도 $K(1 \le K \le 4)$가 공백으로 구분되어 주어진다. 둘째 줄에 과거로 돌아간
www.acmicpc.net
package BOJ.simulation;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_24885 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Function<String,Integer> stoi = Integer::parseInt;
StringTokenizer st = new StringTokenizer(br.readLine());
int n = stoi.apply(st.nextToken());
long money = stoi.apply(st.nextToken());
int k = stoi.apply(st.nextToken());
int[] price = new int[n];
st = new StringTokenizer(br.readLine());
for(int i = 0 ; i < n ; i++){
price[i] = stoi.apply(st.nextToken());
}
long result = cal(price,n,money,k);
System.out.println(result);
}
private static long cal(int[] price,int n,long money,int k){
long rent = 0;
long result = money;
for(int i = 1 ; i < n ; i++){
if(price[i-1] < price[i] && price[i-1] <= (k+1) * money){
rent = money * k;
long cnt = (money + rent) / price[i-1];
money += (price[i] - price[i-1]) * cnt;
continue;
}
if(price[i-1] >= price[i] && price[i-1] <= (k+1) * money){
long temp = (k+1) * money + (price[i] - price[i-1]) * ((k+1) * money/price[i-1]);
result = Math.max(result,temp);
}
}
money += rent;
result = Math.max(result,money);
return result;
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 마법의 엘리베이터(JAVA) (0) | 2023.04.08 |
---|---|
프로그래머스 퍼즐 조각 채우기 (JAVA) (0) | 2023.04.07 |
백준 5566번 주사위 게임 (JAVA) (0) | 2023.04.05 |
프로그래머스 호텔 대실 (JAVA) (0) | 2023.04.04 |
프로그래머스 과제 진행하기 (JAVA) (0) | 2023.04.03 |