[Algorithm/Java] ๋ฐฑ์ค 20546๋ฒ - ๐๊ธฐ์ ์ ๋งค๋งค๋ฒ๐
https://www.acmicpc.net/problem/20546
๐ ๋ฌธ์ ํ์ด
๋ฌธ์ ๋์ํ
๐ป ์ ์ฒด ์ฝ๋
ํด๋์ค ์์ฑํ๋ ๊ฒ์ ๋ ์ต์ํด์ ธ์ผ ํ ๊ฒ ๊ฐ๋ค.
ํด๋์ค ์ฌ์ฉ
import java.io.*;
import java.util.*;
public class Main {
static int[] stockPrice;
static class Investor {
int cash;
int stockCnt;
// ์์ฑ์
public Investor(int cash) {
// ์ฒ์์ ํ๊ธ๋ง ์๊ธฐ ๋๋ฌธ์ cash๋ง ๋ฐ๊ธฐ
this.cash = cash; // ์ด๊ธฐ ์๋ณธ ์ค์
this.stockCnt = 0;// ๋ณด์ ์ฃผ์ ์ 0์ผ๋ก ์ด๊ธฐํ
}
// ๋งค์
public void buy(int todayPrice) {
stockCnt = cash / todayPrice;
cash -= todayPrice * stockCnt;
}
// ๋งค๋
public void sell(int todayPrice) {
cash += todayPrice * stockCnt;
stockCnt = 0;
}
// ์์ฐ ๊ณ์ฐ
public int total(int lastPrice) {
return cash + (stockCnt * lastPrice);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cash = Integer.parseInt(br.readLine());
stockPrice = new int[14];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < 14; i++) {
stockPrice[i] = Integer.parseInt(st.nextToken());
}
int bnp = BNP(cash);
int timing = TIMING(cash);
// System.out.println(timing);
// System.out.println(bnp);
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 < 13; i++) {
investor.buy(stockPrice[i]);
}
return investor.total(stockPrice[13]);
}
static int TIMING(int cash) {
Investor investor = new Investor(cash);
for (int i = 3; i < 13; i++) {
if (stockPrice[i - 1] > stockPrice[i - 2] && stockPrice[i - 2] > stockPrice[i - 3]) {
investor.sell(stockPrice[i]);
} else if (stockPrice[i - 1] < stockPrice[i - 2] && stockPrice[i - 2] < stockPrice[i - 3]) {
investor.buy(stockPrice[i]);
}
}
return investor.total(stockPrice[13]);
}
}
ํด๋์ค ๋ฏธ์ฌ์ฉ
import java.io.*;
import java.util.*;
public class Main {
static int[] stockPrice;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cash = Integer.parseInt(br.readLine());
stockPrice = new int[14];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < 14; i++) {
stockPrice[i] = Integer.parseInt(st.nextToken());
}
int bnp = BNP(cash);
int timing = TIMING(cash);
// System.out.println(timing);
// System.out.println(bnp);
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 stockCnt = 0;
for(int i = 0; i<13; i++){
if(stockPrice[i] <= cash){
// ์ ๋ ๋งค์
int stockBuy = cash / stockPrice[i];
stockCnt += stockBuy;
cash -= stockPrice[i] * stockBuy;
}
}
return cash + stockPrice[13] * stockCnt;
}
static int TIMING(int cash) {
int stockCnt = 0;
for(int i=3; i<13; i++){
// 3์ผ์งธ ์์น -> ์ ๋ ๋งค๋
if(stockPrice[i-1] > stockPrice[i-2] && stockPrice[i-2] > stockPrice[i-3]){
cash += stockCnt * stockPrice[i];
stockCnt = 0;
}
// 3์ผ์งธ ํ๋ฝ -> ์ ๋ ๋งค์
else if(stockPrice[i-1] < stockPrice[i-2] && stockPrice[i-2] < stockPrice[i-3]){
int stockBuy = cash / stockPrice[i];
stockCnt += stockBuy;
cash -= stockPrice[i] * stockBuy;
}
}
return cash + stockPrice[13] * stockCnt;
}
}
๋๊ธ๋จ๊ธฐ๊ธฐ