65,210
社区成员
发帖
与我相关
我的任务
分享#include <iostream.h>
#include <windows.h>
///////////////////////////////////////////////////////////////////////////
int seqsearch(int a[],const int n,const int x)
{
int i;
for(i = 0; i != n; ++i)
if(a[i] == x)
break;
if(i == n)
return -1;
else return i;
}
///////////////////////////////////////////////////////////////////////////
void TimSearch()
{
int a[1001], n;
for(int j = 0; j <= 1000; j++)
a[j] = j + 1; //初始化a
cin >> n; //输入n
/////////////////////////////////////////////计时测试/////////
_LARGE_INTEGER time_start; /*开始时间*/
_LARGE_INTEGER time_over; /*结束时间*/
_LARGE_INTEGER dqFreq; /*计时器频率*/
_LARGE_INTEGER f; /*计时器频率*/
QueryPerformanceFrequency(&f);
dqFreq.QuadPart = f.QuadPart;
QueryPerformanceCounter(&time_start);
int k = seqsearch(a, n, 0); //不成功查找
QueryPerformanceCounter(&time_over);
cout << n << " " << ((time_over.QuadPart-time_start.QuadPart)/(double)(dqFreq.QuadPart)) << "s" << endl;
////////////////////////////////////////////计时测试//////////
}
///////////////////////////////////////////////////////////////////////////
int main()
{
cout << "顺序搜索法..." << endl;
TimSearch();
return 1;
}
/*
测试结果:
顺序搜索法...
900
900 6.4254e-006s
*/