股票收益问题

2024年8月28日 | 14 分钟阅读

“股票跨度问题”属于金融领域。此问题旨在确定每日股票价格的跨度。其跨度是指在任何给定日期之前,股票价格小于或等于该日价格的最大连续天数。

股票价格在某一天的跨度 Si 被描述为:紧接在该日之前,当日股价小于或等于其之前价格的最大连续天数。

例如,如果提供一个为期七天的价格数组为 100, 80, 60, 70, 60, 75, 85,则这七天的跨度值分别为 1, 1, 1, 2, 1, 4, 6。

有两种方法可以解决这个股票跨度问题。这两种方法是:

  • 低效方法
  • 高效方法

在解决这个股票跨度问题时,需要遍历输入的价格数组。当左侧的项较小时,遍历左侧的元素,并增加每个被访问元素的跨度值。

现在,让我们借助用三种不同编程语言(C++、Java 和 Python)编写的代码来理解股票跨度问题。

C++ 代码

让我们通过以下 C++ 代码来理解这个问题。

输出

上述代码给出以下输出。

Enter the size of the span array::
7
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
16
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
67
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
44
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
93 
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
33
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
2
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
86
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
2
Span Values Calculated Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
3
New calculated Span values:
1 2 1 4 1 1 3 
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
n

在上面的 C++ 代码中,我们创建了一个类,该类包含计算所插入股票价格跨度值的函数。代码的流程如下:用户有三个选择:添加股票价格,为这些插入的股票价格计算跨度值,然后打印计算出的股票价格跨度值。用户输入股票价格;价格成功添加后,调用 calculateSpan() 函数,该函数为插入的价格值计算跨度值。最后,通过最后一个选项打印这些跨度值。

Java 代码

让我们也借助 Java 代码来理解这个问题。

输出

上面的 Java 代码产生以下输出。

Enter the size of the span array:: 
9
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
14
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
287
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1 
Enter the price value that you want to add to the array :
32
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
40
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
533
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
6
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
700
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
855
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the price value that you want to add to the array :
9002
Data Added Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
2
Span Values Calculated Successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations listed below::
1. To Insert a new price value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
3
New calculated Span values.
Span of 9 days : 1 2 1 2 5 1 7 8 9 
Do you want to continue (Type y or n)
n

在上面的 Java 代码中,我们创建了一个类,该类包含计算所插入股票价格跨度值的函数。代码的流程如下:用户有三个选择:添加股票价格,为这些插入的股票价格计算跨度值,然后打印计算出的股票价格跨度值。现在,用户输入股票价格。价格成功添加后,调用 calculateSpan() 函数,该函数为插入的价格值计算跨度值。最后,通过最后一个选项打印这些跨度值。

Python

现在我们通过这段 Python 代码来找到这个问题的解决方案。

输出

以上 Python 代码产生以下输出。

Enter the size of the span array::
7
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
16
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
67
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
44
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
93 
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
33
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
2
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
1
Enter the span value that you want to add to the array :
86
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
2
Span Values Calculated Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations listed below::
1. To Insert a new span value into the array.
2. To Calculate span value entered in array.
3. To Display the Calculated span values.
3
New calculated Span values:
1 2 1 4 1 1 3 
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
n

在上面的 Python 代码中,我们创建了一个类,该类包含计算所插入股票价格跨度值的函数。代码的流程如下:用户有三个选择:添加股票价格,为这些插入的股票价格计算跨度值,然后打印计算出的股票价格跨度值。现在,用户输入股票价格。价格成功添加后,调用 calculateSpan() 函数,该函数为插入的价格值计算跨度值。最后,通过最后一个选项打印这些跨度值。


下一个主题树堆数据结构