백준 20546 기적의 매매법[Java]

seren-dev·2022년 9월 5일
0

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

풀이

준현이와 성민이가 주식을 매매하는 방법은 다르다. 이를 함수로 구현하고, 배열에 주식 가격을 저장한다. 단, 마지막 날의 주식 가격은 따로 저장한다. 즉, arr 배열에는 13일까지의 주식 가격만 저장한다.
주식 가격의 상승/하락 여부는 boolean형 변수로 나타낸다.

코드

import java.io.*;
import java.util.*;

public class Main {

    public static int funcOne(int n, int[] arr, int last) {
        int cnt = 0;
        for (int price : arr) {
            if (n >= price) {
                cnt += n / price;
                n %= price;
            }
        }
        return n + cnt * last;
    }

    public static int funcTwo(int n, int[] arr, int last) {
        boolean upOne = false, upTwo = false, downOne = false, downTwo = false;
        int cnt = 0;

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > arr[i-1]) {
                downOne = downTwo = false;
                if (!upOne)
                    upOne = true;
                else if (!upTwo)
                    upTwo = true;
                else {
                    n += arr[i] * cnt;
                    cnt = 0;
                }
            }
            else if (arr[i] < arr[i-1]) {
                upOne = upTwo = false;
                if (!downOne)
                    downOne = true;
                else if (!downTwo)
                    downTwo = true;
                else {
                    cnt += n / arr[i];
                    n %= arr[i];
                }
            }
        }

        return n + cnt * last;
    }

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] arr = new int[13];

        StringTokenizer st = new StringTokenizer(br.readLine());

        for (int i = 0; i < 13; i++)
            arr[i] = Integer.parseInt(st.nextToken());
        int last = Integer.parseInt(st.nextToken());

        int first = funcOne(n, arr, last);
        int second = funcTwo(n, arr, last);

        if (first == second)
            System.out.println("SAMESAME");
        else
            System.out.println(first > second ? "BNP" : "TIMING");

    }
}

다른 풀이

boolean형 변수를 사용하지 않고, 주식이 상승/하락할 때마다 upCnt/downCnt에 1을 더한다.

import java.io.*;
import java.util.*;

public class Main {

    public static int funcOne(int n, int[] arr, int last) {
        int cnt = 0;
        for (int price : arr) {
            if (n >= price) {
                cnt += n / price;
                n %= price;
            }
        }
        return n + cnt * last;
    }

    public static int funcTwo(int n, int[] arr, int last) {
        int cnt = 0, upCnt = 0, downCnt = 0;

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > arr[i-1]) {
                downCnt = 0;
                if (upCnt < 2)
                    upCnt++;
                else {
                    n += arr[i] * cnt;
                    cnt = 0;
                }
            }
            else if (arr[i] < arr[i-1]) {
                upCnt = 0;
                if (downCnt < 2)
                    downCnt++;
                else {
                    cnt += n / arr[i];
                    n %= arr[i];
                }
            }
        }

        return n + cnt * last;
    }

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] arr = new int[13];

        StringTokenizer st = new StringTokenizer(br.readLine());

        for (int i = 0; i < 13; i++)
            arr[i] = Integer.parseInt(st.nextToken());
        int last = Integer.parseInt(st.nextToken());

        int first = funcOne(n, arr, last);
        int second = funcTwo(n, arr, last);

        if (first == second)
            System.out.println("SAMESAME");
        else
            System.out.println(first > second ? "BNP" : "TIMING");

    }
}


첫번째 방법과 두번째 방법의 메모리와 실행시간의 차이가 크지 않다.

0개의 댓글