-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockCalc.java
66 lines (57 loc) · 1.85 KB
/
StockCalc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Java Programming
// 2021.04.16
public class StockCalc {
public static void main(String[] args) {
System.out.println("최종가격: "+ new StockCalc().stock(new int[]{4, 3, 7, 5, 4, 2, 3, 5, 6, 8, 5, 4, 3, 6, 4, 5, 8, 2, 3}, 20));
}
public int stock(int[] T, int w) {
int answer = w;
int stockNum = 0;
int[] checkStock = new int[T.length];
for(int i = 0; i<checkStock.length-1;i++){
checkStock[i] = T[i+1]-T[i];
if(checkStock[i] == 0)
checkStock[i] = checkStock[i-1];
}
checkStock[checkStock.length-1] = checkStock[checkStock.length-2];
for (int i = 0; i < T.length; i++) {
// 처음 주식 결정
if(i == 0){
if(checkStock[i] < 0)
continue;
stockNum = answer / T[i];
answer %= T[i];
continue;
}
// 마지막 주식 결정
if (i == T.length-1) {
if(checkStock[i] >= 0){
answer += stockNum * T[i];
stockNum = 0;
}
continue;
}
// if (i + 1 == T.length) {
// if(checkStock[i] > 0){
// answer += stockNum * T[i];
// stockNum = 0;
// }
// continue;
// }
// 중간 주식 결정
// 판매
if (checkStock[i-1] > 0 && checkStock[i] <0) {
answer += stockNum * T[i];
stockNum = 0;
continue;
}
// 구매
if (checkStock[i-1] < 0 && checkStock[i] > 0) {
stockNum = answer / T[i];
answer %= T[i];
continue;
}
}
return answer;
}
}