第1关:顺序表逆置
#include<iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;typedef struct SqList
{int *elem;int length;
}SqList;void ListReverse(SqList &L)
{// 请在此编写代码for(int i=0;i<L.length/2;i++){swap(L.elem[i],L.elem[L.length-i-1]);}
}
第2关:顺序表删除指定范围值的元素
#include<iostream>
#include<vector>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;typedef struct SqList
{int *elem;int length;
}SqList;void Delete_s_t(SqList &L,int s,int t)
{//请在此编写代码//注意删除以后修改顺序表长vector<int>v;int cnt=0;for(int i=0;i<L.length;i++){int x=L.elem[i];if(x>=s&&x<=t)continue;v.push_back(x);cnt++;}for(int i=0;i<cnt;i++)L.elem[i]=v[i];L.length=cnt;
}
第3关:顺序表最长连续递增子序列
#include<iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;typedef struct SqList
{int *elem;int length;
}SqList;void findLengthOfLCIS(SqList L)
{// 请在此编写代码int n=L.length;int left=0;int right=1;int ret=0;int x=0,y=0;for(right=1;right<n;right++){if(L.elem[right]<=L.elem[right-1]){int cnt=right-left;if(cnt>ret){ret=cnt;x=left,y=right-1;}left=right;}}int cnt=n-left;if(cnt>ret){ret=cnt;x=left,y=n-1;}for(int i=x;i<=y;i++){cout<<L.elem[i]<<" ";}
}
第4关:删除有序顺序表中的重复项
#include<iostream>
#include<vector>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;typedef struct SqList
{int *elem;int length;
}SqList;int removeDuplicates(SqList &L)
{// 请在此编写代码vector<int>v;v.push_back(L.elem[0]);for(int i=1;i<L.length;i++){if(L.elem[i]==L.elem[i-1])continue;v.push_back(L.elem[i]);}int n=v.size();for(int i=0;i<n;i++){L.elem[i]=v[i];}L.length=n;return n;
}