# Classroom ###### tags: `Date:1110126` ### [Problem 01](https://zerojudge.tw/ShowProblem?problemid=h081)  #### Problem Arrangement: - Input : n : 陣列長度 D : 判斷值 a[i] : 價格 - 條件 : 根據題目所給的股票價格陣列,判斷可獲利總額 1.第一個價格必買入 2.狀態 : 持有股票 / 賣出股票 3.持有 => if(股票價格>=現價格+D):賣出 4.未持有 => if(股票價格<=上次賣出價格-D):買入 - Output: sum : 獲利總和 #### Example IO: - Input 01: ``` 3 10 50 20 45 ``` - Output 01: ``` 0 ``` - Input 02: ``` 6 10 30 20 45 38 10 20 ``` - Output 02: ``` 25 ``` - 提示 : `範例 1` >總共 `3` 個時間點,於時間點 `1` 花費 `50` 買進股票,但後續沒有任何時間點超過 `50` 元,因此總利潤為 `0` `範例 2` > 總共 `6` 個時間點,於時間點 `1` 花費 `30` 買進股票,並於時間點 `3` 得到 `45` 賣出獲得利潤為 `15`,再於時間點 `5` 花費 `10` 買進,並於時間點 `6` 賣出獲得利潤為 `10`,總利潤為 `25` #### Hint: A.需宣告的element: 1.price array : vector、array皆可 > type : array 2.n,D : 陣列長度、判斷值 > type : int 3.status : 狀態(持有/未持有) > type : bool 4.q : 價格log > type : int 5.sum : 獲利總和 > type : long long B.流程圖: 1.輸入n,D,a[n] 2.線性搜尋判斷 a.狀態 b.是否通過條件 3.執行個狀況所需命令 4.輸出結果 #### Example Ans: ```cpp= #include<bits/stdc++.h> #define spdup ios::sync_with_stdio(0),cin.tie(0) #define ll long long using namespace std; main(){ spdup; //define variable int n,d,q; bool status; ll sum; while(cin>>n>>d){ vector<int> price(n); // Que 1 for(int &x:price)cin>>x; //Que2 q=*price.begin(); status=true; sum=0; for(auto x=price.begin();x!=price.end();x++){ if(status){ if(*x>=q+d){ sum+=(*x-q); q=*x; status=false; } } else{ if(*x<=q-d){ status=true; q=*x; } } } cout<<sum<<'\n'; } } ``` ###### tags: `Date:1110204` ### [Problem 02](https://zerojudge.tw/ShowProblem?problemid=e799)  #### Example Ans: ```cpp= #include<bits/stdc++.h> #define spdup ios::sync_with_stdio(0),cin.tie(0) #define ll long long using namespace std; main(){ spdup; ll x,n,m,tmp; char a; while(cin>>n>>m>>a){ while(n--){ vector<int> g(m,0); cin>>tmp; for(x=1;x<=m;x++){ g[m-x]=tmp%2; tmp/=2; } for(x=0;x!=m;x++){ cout<<(g[x]==1?a:'.')<<' '; } cout<<'\n'; } } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up