best time to buy and sell stocks

Source: Internet
Author: User
Topic:

Given an array, the number of I represents the price of the day I, please select a strategy to make the most profit. can only buy and sell once a day

Answer:

Find all the rising bands, buy the first day of each interval, sell the last day

input int a[], length

BOOL Haspos = false; Whether there is an open position

for (int i = 0; i < length-1; i++)
{
if (A[i + 1] < A[i])//Tomorrow price falls
{
if (haspos) a[i] = -1;//sell If you have an open position
else a[i] = 0; No position, no movement.
Haspos = false;
}
else if (a[i + 1] > A[i])//Price rises tomorrow
{
if (haspos) a[i] = 0; You can't move with a position.
else a[i] = 1; Buy it without a position.
Haspos = true;
}
else a[i] = 0; The price doesn't change, it doesn't move.
}


A[length-1] = Haspos? -1:0; On the last day, we sell it with open positions.

Get a set of [0, 1, 0, 0,-1, ...] The result of the encounter 1 on the buy, encountered-1 on the Sell

best time to buy and sell stocks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.