算法设计4-5

第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;	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值