#include<iostream>
using namespace std;
#include<time.h>
#define n 10
int main()
{
time_t ts;
srand((unsigned int)time(&ts)); //设置随机数种子
int a[n]={0};
for(int i=1;i<n;i++)
{
a[i]=rand()%60; //产生随机数
}
int m;
cout<<"请输入查找的数据\n";
cin>>m;
a[0]=m; //设置监视哨,避免每次循环都判断数组是否越界,从而节省时间
int i=n-1; //从数组尾向前查找
while(a[i]!=m)
{
i--;
}
if(i!=0)
{
cout<<"该数据位置:"<<i<<endl;
}else
{
cout<<"error\n";
}
return 0;
}
查找算法之顺序查找
最新推荐文章于 2023-01-29 11:00:04 发布