pellen吧 关注:13贴子:237
  • 3回复贴,共1

实验七第一题题目

只看楼主收藏回复

#include<iostream.h>
#define max 100
#define ok 1 typedef int type;
typedef struct {type elem[max];
int length;
}stable; void creatlist(stable L,int n)
{int i;
cout<<"Please input the number of list:";
for(i=0;i<n;i++)
cin>>L->elem[i];
L->length=n;
} void showlist(stable L)
{int i;
for(i=1;i<L.length ;i++)//按序号输出查找的序列
{cout<<i<<"--"<<L.elem[i];
cout<<endl;}
}
//search_1()补充顺序查找算法
main()
{int m,x;
stable T ;
type key;
cout<<"please input the M:";
cin>>m;
cout<<"\n";//调用上面的建表函数完成建立顺序表
cou<<"\n";
showlist(T);
cout<<"I want to find the:";
cin>>key;
cout<<"\n";
//x=search_1(T,key),//顺序查找并输出key的位置,注意参数的变化。
cout<<"The key find,search position is:"<<x<<"\n";
}


IP属地:浙江1楼2012-12-06 16:01回复
    #include<iostream.h>
    #define max 100
    typedef int type;
    typedef struct{
    type elem[max];
    int length;
    }stable;
    void creatlist(stable &L,int n)
    {
    int i;
    cout<<"please input number of list";
    for(i=0;i<n;i++)
    cin>>L.elem[i];
    L.length=n;
    } void showlist(stable L)
    {
    int i;
    for(i=1;i<=L.length;i++)
    {
    cout<<i<<"--"<<L.elem[i];
    cout<<endl;
    }
    } int search(stable L,int key)
    { int low,high,mid;
    low=1;high=L.length;
    while(low<=high)
    {
    mid=(low+high)/2;
    if(key==L.elem[mid]) return mid+1;
    else
    if(key<L.elem[mid]) high=mid-1;
    else low=mid+1;
    }
    return 0;
    } void main()
    {
    int m,x;
    stable L;
    int key;
    cout<<"input m"<<endl;
    cin>>m;
    cout<<"\n";
    creatlist(L,m);
    cout<<"input key"<<endl;
    cin>>key;
    x=search(L,key);
    if(x!=0)
    cout<<"key is"<<x<<"\n";
    else cout<<"Not find"; }


    IP属地:湖北2楼2012-12-06 16:53
    回复
      int search(stable L,int key)
      { int i;
      for(i=0;i<L.length;i++)
      {
      if(L.elem[i]==key)
      return i+1;
      }
      return 0;


      IP属地:湖北3楼2012-12-06 16:56
      收起回复