第1关:调整负值
#include <iostream>
using namespace std;
# define MAXSIZE 20 //设记录不超过20个
typedef struct { //定义顺序表的结构
int r[MAXSIZE+1]; //存储顺序表的向量 r[0]一般作哨兵或缓冲区
int length ; //顺序表的长度
}SqList;
int ListCreate(SqList &L)
{
int i=1,n;
cin>>n;
while(i<=n)
{
cin>>L.r[i];
i++;
}
L.length=n;
}
void process(SqList &L)
{
//###### Begin ######
int i = 1,j = L.length;
while(i < j)
{
while(i < j && L.r[i] < 0) i ++ ;
while(i < j && L.r[j] >= 0) j -- ;
if(i < j)
{
int t = L.r[i];
L.r[i] = L.r[j];
L.r[j] = t;
}
}
// ###### End ######
}
void ListOut(SqList L)
{
int i;
for(i=1;i<=L.length;i++)
cout<<L.r[i]<<" ";
cout<<endl;
}
int main()
{
SqList L;
ListCreate(L);
process( L );
ListOut(L);
return 0;
}
第2关:快排查找
#include <iostream>
using namespace std;
# define MAXSIZE 20 //设记录不超过20个
# define OK 1;
typedef int datatype;
typedef struct
{
datatype key;
}RecType;
typedef struct { //定义顺序表的结构
RecType r[MAXSIZE+1]; //存储顺序表的向量 r[0]一般作哨兵或缓冲区
int length ; //顺序表的长度
}SqList;
void ListCreate(SqList &L)
{
int i=1,n;
cin>>n;
while(i<=n)
{
cin>>L.r[i].key;
i++;
}
L.length=n;
}
int index (RecType R[],int low,int high,datatype key) //key为待查关键字
{
//###### Begin ######
while(low <= high)
{
while(low < high && R[high].key > key) high -- ;
if(R[high].key == key) return high;
while(low < high && R[low].key < key) low ++ ;
if(R[low].key == key) return low;
low ++,high -- ;
}
cout << "Not find" << endl;
return 0;
// ###### End ######
}//index
int main()
{
SqList L;
datatype key;
int loc;
ListCreate(L);
cin>>key;
loc=index(L.r,1,L.length,key); //loc是关键字所在位置
cout<<loc<<endl;
}