2 ๋ถ„ ์†Œ์š”



https://www.acmicpc.net/problem/20546



๐Ÿ” ๋ฌธ์ œ ํ’€์ด

๋ฌธ์ œ ๋„์‹ํ™”

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;
    }
}


์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ