1 // ch09_01.cpp : 《数据结构》顺序查找法
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <iomanip>
 7 #include <cstdlib>
 8 using namespace std;
 9 
10 
11 int _tmain(int argc, _TCHAR* argv[])
12 {
13     int data[80],val;
14     int find=0;
15     for (int i=0;i<80;i++)
16         data[i]=rand()%150+1;
17 
18     cout<<"==========数据内容:========="<<endl;
19     for (int i=0;i<10;i++)
20     {
21         for (int j=0;j<8;j++)
22             cout<<setw(2)<<i*8+j+1 <<" ["<<setw(3)<<data[i*8+j]<<"";
23         cout<<endl;
24     }
25 
26     cout<<"请输入搜索键值";
27     cin>>val;
28     while(val!= -1)
29     {
30         for (int i=0;i<80;i++)
31             if (val==data[i])
32                 cout<<""<<i+1<<"的位置找到了键值"<<data[i]<<endl;
33                 find++;
34                 break;
35     
36     }
37     if (find==0)
38         cout<<"没有找到键值.";
39     
40     cout<<"==========数据内容:========="<<endl;
41     for (int i=0;i<10;i++)
42     {
43         for (int j=0;j<8;j++)
44             cout<<setw(2)<<i*8+j+1 <<" ["<<setw(3)<<data[i*8+j]<<"";
45         cout<<endl;
46     }
47     system("pause");
48     return 0;
49 }

Rand Posts: