#32971: 各位大神請問我錯在哪


NIMASILA (159333劉正堯)

學校 : 國立臺灣師範大學附屬高級中學
編號 : 199679
來源 : [114.37.177.131]
最後登入時間 :
2024-03-12 20:32:42
h081. 1. 程式交易 -- 2022年1月APCS | From: [114.37.182.118] | 發表日期 : 2022-11-20 11:01

#include<iostream>
using namespace std;
int a[105];
int main(){
    int n,D;
    cin>>n>>D;
    for(int i = 0;i < n;i++) cin>>a[i];
    int stock = a[0],profit = 0,last_sell = 0,having = 1;
    for(int i = 1;i < n;i++)
    {
        if(a[i] >= stock + D && having == 1)
        {
            profit += a[i] - stock;
            last_sell = stock;
            having = 0;
        }
        if(a[i] <= stock - D && having == 0)
        {
            stock = a[i];
            having = 1;
        }
    }
    cout<<profit;

 
#32972: Re: 各位大神請問我錯在哪


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
h081. 1. 程式交易 -- 2022年1月APCS | From: [118.231.185.180] | 發表日期 : 2022-11-20 14:15


        if(a[i] >= stock + D && having == 1)
        {
            profit += a[i] - stock;
            last_sell = stock;
            having = 0;
        }


這裡也要加一個 stock = a[i];

 
ZeroJudge Forum