2 λΆ„ μ†Œμš”



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



πŸ” 문제 풀이

문제 도식화

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


μΉ΄ν…Œκ³ λ¦¬:

μ—…λ°μ΄νŠΈ:

λŒ“κΈ€λ‚¨κΈ°κΈ°