[Algorithm/Java] λ°±μ€ 20546λ² - πκΈ°μ μ λ§€λ§€λ²π
https://www.acmicpc.net/problem/20546
π λ¬Έμ νμ΄
λ¬Έμ λμν
π» μ 체 μ½λ
ν΄λμ€ μμ±νλ κ²μ λ μ΅μν΄μ ΈμΌ ν κ² κ°λ€.
ν΄λμ€ μ¬μ©
import java.io.*;
import java.util.*;
public class Main {
static int[] stockChart;
static class Investor {
int cash;
int stock;
// μμ±μ
public Investor(int cash) {
this.cash = cash;
this.stock = 0; // 보μ μ£Όμ 0μΌλ‘ μ΄κΈ°ν
}
// λ§€μ
public void buy(int todayPrice) {
if (todayPrice <= cash) {
int count = cash / todayPrice;
stock += count;
cash -= count * todayPrice;
}
}
// λ§€λ
public void sell(int todayPrice) {
cash += stock * todayPrice;
stock = 0;
}
// μμ° κ³μ°
public int asset(int currentPrice) {
return cash + stock * currentPrice;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cash = Integer.parseInt(br.readLine());
stockChart = new int[14];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<14; i++){
stockChart[i] = Integer.parseInt(st.nextToken());
}
int bnp = BNP(cash);
int timing = TIMING(cash);
if (bnp < timing) System.out.println("TIMING");
else if (timing < bnp) System.out.println("BNP");
else System.out.println("SAMESAME");
}
static int BNP(int cash){
Investor investor = new Investor(cash); // κ°μ²΄ μμ±
//μ£Όμ μ λ μνμ, μ΄ μ μμΌλ©΄ μ΅λν λ§μ΄ λ§€μ
for(int i = 0; i < 14; i++){
if(stockChart[i] <= cash){
investor.buy(stockChart[i]); // μ λλ§€μ
investor.asset(stockChart[i]); // νμ¬ μμ°
}
}
return investor.asset(stockChart[13]);
}
// μ±λ―Όμ΄λ νμ΄λ°
// 1. 3μΌμ°μ μ£Όμ μμΉνλ©΄ μ λ λ§€λ
// 2. 3μΌμ°μ μ£Όμ νλ½νλ©΄ μ λ λ§€μ
static int TIMING(int cash){
Investor investor = new Investor(cash); // κ°μ²΄ μμ±
for(int i = 3; i < 14; i++){
// 3μΌ μ°μ μ£Όμ μμΉ
if(stockChart[i-1] > stockChart[i-2] && stockChart[i-2]> stockChart[i-3]){
// μ λ λ§€λ
investor.sell(stockChart[i]);
}
// 3μΌ μ°μ μ£Όμ νλ½
else if(stockChart[i-1] < stockChart[i-2] && stockChart[i-2] < stockChart[i-3]){
// μ λ λ§€μ
investor.buy(stockChart[i]);
}
}
return investor.asset(stockChart[13]);
}
}
ν΄λμ€ λ―Έμ¬μ©
import java.io.*;
import java.util.*;
public class Main {
static int[] stockChart;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cash = Integer.parseInt(br.readLine());
stockChart = new int[14];
// μ
λ ₯
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<14; i++){
stockChart[i] = Integer.parseInt(st.nextToken());
}
// νΈμΆ
int bnp = BNP(cash);
int timing = TIMING(cash);
// μΆλ ₯
if (bnp < timing) System.out.println("TIMING");
else if (timing < bnp) System.out.println("BNP");
else System.out.println("SAMESAME");
}
static int BNP(int cash){
// 무쑰건 λλλλλ‘ μ λλ§€μ
int stock = 0;
for(int i=0; i<14; i++){
if(cash >= stockChart[i]){
int buy = cash/stockChart[i]; // μ€λ μ΄ μ μλ μ£Όμ μ
stock += buy; // λμ μ£Όμ
cash -= buy * stockChart[i]; // μ€λ μ° λ§νΌλ§ μ°¨κ°
}
}
return cash + stockChart[13] * stock;
}
static int TIMING(int cash){
int stock = 0;
for(int i=3; i<14; i++){
// μ£Όμ 3μΌ μ°μ μμΉ
if(stockChart[i-1] > stockChart[i-2] && stockChart[i-2] > stockChart[i-3]){
// μ λ λ§€λ
cash += stock * stockChart[i];
stock = 0;
}
// μ£Όμ 3μΌ μ°μ νλ½
else if(stockChart[i-1] < stockChart[i-2] && stockChart[i-2] < stockChart[i-3]){
// μ λ λ§€μ
int buy = cash/stockChart[i]; // μ€λ μ΄ μ μλ μ£Όμ μ
stock += buy; // λμ μ£Όμ
cash -= buy * stockChart[i]; // μ€λ μ° λ§νΌλ§ μ°¨κ°
}
}
return cash + stockChart[13] * stock;
}
}
λκΈλ¨κΈ°κΈ°