###### tags: `Zerojudge` 【題解】apcs h081: 1.程式交易 === Posted on 2022/4/28 | By : Victoria1103 【題目敘述】https://zerojudge.tw/ShowProblem?problemid=h081 ```cpp= #include <iostream> using namespace std; int main () { int n , D ; cin >> n >> D ; int a[n] ; for (int i = 0 ; i < n ; i++ ) { cin >> a[i] ; } int have = 1 , x = a[0] , y = 0 ;//持有股票 int ans = 0 ; for (int i = 1; i < n ; i++) { if (have==1 && a[i]-x>=D){ y = a[i]; ans += y-x;//利潤 have=0; } else if(have==0 && y-a[i]>=D){ x = a[i]; have=1; } } cout << ans ; return 0; }