计算机考研机试指南(五)——查找

编程日记 cha2-05 查找

找x

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <stdio.h>
 5 #include <iomanip>
 6 using namespace std;
 7 /*
 8     题目:找x
 9     用时:tomato *
10     思路:
11 
12 
13 */
14 
15 int main()
16 {
17    int n,a[201],x;
18    bool flag = true;
19    while (cin >> n )
20    {
21        flag = true;
22        for (int i=0;i<n;i++)
23         cin >> a[i];
24        cin >> x;
25        for (int i=0;i<n;i++)
26             if (a[i] == x)
27             {
28                 cout<<i<<endl;
29                 flag = false;
30             }
31 
32         if (flag)
33             cout<<-1<<endl;
34    }
35 
36 
37 
38     return 0;
39 }

查找学生信息

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <string>
 5 #include <stdio.h>
 6 #include <iomanip>
 7 using namespace std;
 8 /*
 9     题目:查找学生信息
10     用时:tomato *
11     思路:二分法(序号是有序的)
12     为什么不能用普通的查找方法:时间复杂度o(m*n):学生个数*需要查找的次数
13     问题:strlen比较的时候总相反,猜想是因为char类型没有初始化的缘故
14     解决:最后发现因为没有sort排序的缘故,为什么一定要排序呢?????
15     难点:重载大于运算符运用在sort中,看c++的书
16 
17 
18 */
19 struct Student
20 {
21     char num[5];
22     char name[10];
23     char gender[5];
24     int age;
25     bool operator < (const Student & A) const{
26     return strcmp(num,A.num)<0;
27     }
28 }stu[1001];
29 
30 int binarySearch(char x[],int n)
31 {
32     // 在stu中根据二分法查找number x
33     int low = 0,high = n-1;
34     char temp[5];
35     while ( low <= high )
36     {
37         int mid = ( low + high )/2;
38       //  cout << "mid num:" << stu[mid].num <<endl;
39 //        strcpy(temp,stu[mid].num);
40         if (strcmp(stu[mid].num , x)>0 )
41         {
42             // x比中间小,区间变成左边
43             high = mid -1;
44         }
45         if (strcmp( stu[mid].num , x)<0)
46         {
47             low = mid + 1;
48         }
49         if (strcmp (stu[mid].num , x ) == 0)
50             return mid;
51 
52     }
53     return -1;
54 }
55 
56 int main()
57 {
58 
59    int n;
60    int m;
61    char x[5];
62 
63    while (cin>>n)
64    {
65        for (int i=0;i<n;i++)
66        {
67            scanf("%s %s %s %d",&stu[i].num,&stu[i].name,&stu[i].gender,&stu[i].age);
68        }
69        sort(stu,stu+n);
70        cin >> m;
71        for (int i=0;i<n;i++)
72             cout <<stu[i].num<<' '<<stu[i].name<<' '<<stu[i].gender<<' '<<stu[i].age<<endl;
73 
74        for (int i=0 ; i<m ; i++)
75        {
76            cin>>x;
77            int result_i = binarySearch(x,n);
78            if (result_i == -1)
79                 cout<<"No Answer!"<<endl;
80            else
81                 cout <<stu[result_i].num<<' '<<stu[result_i].name<<' '<<stu[result_i].gender<<' '<<stu[result_i].age<<endl;
82         }
83    }
84 
85 
86 
87     return 0;
88 }

打印极值点下标

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <string>
 5 #include <stdio.h>
 6 #include <iomanip>
 7 using namespace std;
 8 /*
 9     题目:打印极值点下标
10     用时:tomato *
11     思路:经过观察,只需比较中间值和两侧数据的大小:比两侧都大或比两侧都小
12     首尾数据比较特殊:只要不和旁边的数据相等则输出
13     注意:打印的时候最后一个数字后面没有空格,因此我把所有即将被打印的索引存放到数组中,最后一起输出
14 
15 
16 
17 
18 */
19 
20 
21 int main()
22 {
23     int k,a[80];
24     int x[80];
25     int cur = 0;
26     while (cin>>k)
27     {
28         for (int i=0;i<k;i++)
29             cin>>a[i];
30 
31         for (int i=0;i<k;i++)
32         {
33             if (i==0 && a[i+1]!=a[i])
34             {
35                 x[cur] = i;
36                 cur ++;
37                 continue;
38             }
39             if (i==k-1 && a[i]!=a[i-1])
40             {
41                 x[cur] = i;
42                 cur ++;
43                 break;
44             }
45             if ((a[i-1] < a[i] && a[i+1] < a[i]) || (a[i-1] > a[i] && a[i+1] > a[i]))
46             {
47                 x[cur] = i;
48                 cur ++;
49             }
50         }
51         for (int i=0;i<cur-1;i++)
52         {
53             cout << x[i]<<' ';
54         }
55         cout <<x[cur-1];
56 
57     }
58 
59 
60 
61     return 0;
62 }
原文地址:https://www.cnblogs.com/twomeng/p/9509454.html