有大佬能告訴我要怎麼改嗎?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,d;
scanf("%d %d", &n , &d);
int money[n];
for(int i = 0 ;i<n ;i++){
scanf("%d", &money[i]);
}
int now = money[0];
int buy_sale = -1;
int getting = 0;
for(int i = 1 ;i<n ;i++){
if(buy_sale == -1){
if(money[i] - now >=d){
getting += money[i] - now;
buy_sale*=-1;
}
}else{
if(now - money[i] >= d){
now = money[i];
buy_sale *= -1;
}
}
}
printf("%d", getting);
return 0;
}
有大佬能告訴我要怎麼改嗎?
#include
using namespace std;
int main()
{
int n,d;
scanf("%d %d", &n , &d);
int money[n];
for(int i = 0 ;i
scanf("%d", &money[i]);
}
int now = money[0];
int buy_sale = -1;
int getting = 0;
for(int i = 1 ;i
if(buy_sale == -1){
if(money[i] - now >=d){
getting += money[i] - now;
buy_sale*=-1;
}
}else{
if(now - money[i] >= d){
now = money[i];
buy_sale *= -1;
}
}
}
printf("%d", getting);return 0;
}
`now` 的更新沒有處理好