首页 > 编程知识 正文

c语言递归算法求数组最大值,c++最大公约数函数

时间:2023-05-05 08:03:27 阅读:26881 作者:2149

题目(单调队列的应用):

在长度为n的整数数列上,在长度为k的窗口中从左向右滑动,每次滑动一个单位,求出滑动后各窗口中包含的数量的最大值。 例如,在数列为[1、3、-1、-3、5、3、6、7]情况下,窗口尺寸k=3,

窗口位置

窗口中的最大值

[13-1]-35367

3

1[3-1-3]5367

3

13[-1-35]367

5

13-1[-353]67

5

13-1-3[536]7

6

13-1-35[367]

7

样例输入:

8 )分别为n和k ) )。

13-1-35367 (整数数列) )

输出样例:

335567 (窗口中的最大值) )。

//单调队列的应用//author : Mitchell _ Donovan//date :2021.3.22 # includeiostreamusingnamespacestd; classarrayqueue { private : intsize; int front; int tail; int * *队列; 公共:阵列队列(intsize value ) {size=sizeValue 1; 队列=new int * [ size ]; for(intI=0; i size; I ) {queue[i]=new int[2]; 队列[ I ] [0]=队列[ I ] [1]=int _ max; (}前端=0; tail=0 }~arrayQueue () for(intI=0; i size; I ) {delete[]queue[i]; }delete[]queue; }boolpush(intrank,int numberValue ) if ) ((tail1) % size )==front ) {cout 'The queue has been full!' endl; 返回假; }tail=(tail1) % size; 队列[ tail-1 ] [0]=rank; 队列[ tail-1 ] [1]=number value; 返回真; }bool poptail () if ) front==tail ) {cout 'The queue is empty!' endl; 返回假; } queue [ tail-1 ] [0]=queue [ tail-1 ] [1]=int _ max; tail=(sizetail-1 ) % size; 返回真; }bool popfront () if ) front==tail ) ) {cout 'The queue is empty!' endl; 返回假; } queue [ front ] [0]=queue [ front ] [1]=int _ max; 前端=(前端1 ) % size; 返回真; }boolgetmax(int*Array,int arraySize,int testSize ) for ) intI=0; i testSize - 1; I ) if(array(I )=queue((sizetail-1 ) size ) ) while (array ) I )=queue((sizetail-1 ) size ) ) ) }for(intI=testsize-1; I阵列大小; I ) if(I-queue[front][0]=testsize ) {popfront ); //清空过期元素if (array[I]=queue () sizetail-1 ) %size ()1) ) while ) array [ I ]=queue () sizetail-1 ) ) cout queue[front][1] '; }返回真; }; int main () {int arraySize,testSize; cout ' pleaseinputthelengthofarray : '; cin arraySize; cout ' pleaseinputthelengthoftestwindow : '; cin testSize; int* array=new int[arraySize]; cout ' pleaseinputthevalueofeachnumber : '; for(intI=0; I阵列大小; I ) {cin array[i]; }arrayqueuetest(testsize; test.getmax(Array,arraySize,testSize ); }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。