计算机考研机试指南(五)

编程日记 cha2-05 查找

找x

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <iomanip>
using namespace std;
/*
    题目:找x
    用时:tomato *
    思路:


*/

int main()
{
   int n,a[201],x;
   bool flag = true;
   while (cin >> n )
   {
       flag = true;
       for (int i=0;i<n;i++)
        cin >> a[i];
       cin >> x;
       for (int i=0;i<n;i++)
            if (a[i] == x)
            {
                cout<<i<<endl;
                flag = false;
            }

        if (flag)
            cout<<-1<<endl;
   }



    return 0;
}

查找学生信息

#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <stdio.h>
#include <iomanip>
using namespace std;
/*
    题目:查找学生信息
    用时:tomato *
    思路:二分法(序号是有序的)
    为什么不能用普通的查找方法:时间复杂度o(m*n):学生个数*需要查找的次数
    问题:strlen比较的时候总相反,猜想是因为char类型没有初始化的缘故
    解决:最后发现因为没有sort排序的缘故,为什么一定要排序呢?????
    难点:重载大于运算符运用在sort中,看c++的书


*/
struct Student
{
    char num[5];
    char name[10];
    char gender[5];
    int age;
    bool operator < (const Student & A) const{
    return strcmp(num,A.num)<0;
    }
}stu[1001];

int binarySearch(char x[],int n)
{
    // 在stu中根据二分法查找number x
    int low = 0,high = n-1;
    char temp[5];
    while ( low <= high )
    {
        int mid = ( low + high )/2;
      //  cout << "mid num:" << stu[mid].num <<endl;
//        strcpy(temp,stu[mid].num);
        if (strcmp(stu[mid].num , x)>0 )
        {
            // x比中间小,区间变成左边
            high = mid -1;
        }
        if (strcmp( stu[mid].num , x)<0)
        {
            low = mid + 1;
        }
        if (strcmp (stu[mid].num , x ) == 0)
            return mid;

    }
    return -1;
}

int main()
{

   int n;
   int m;
   char x[5];

   while (cin>>n)
   {
       for (int i=0;i<n;i++)
       {
           scanf("%s %s %s %d",&stu[i].num,&stu[i].name,&stu[i].gender,&stu[i].age);
       }
       sort(stu,stu+n);
       cin >> m;
       for (int i=0;i<n;i++)
            cout <<stu[i].num<<' '<<stu[i].name<<' '<<stu[i].gender<<' '<<stu[i].age<<endl;

       for (int i=0 ; i<m ; i++)
       {
           cin>>x;
           int result_i = binarySearch(x,n);
           if (result_i == -1)
                cout<<"No Answer!"<<endl;
           else
                cout <<stu[result_i].num<<' '<<stu[result_i].name<<' '<<stu[result_i].gender<<' '<<stu[result_i].age<<endl;
        }
   }



    return 0;
}

打印极值点下标

#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <stdio.h>
#include <iomanip>
using namespace std;
/*
    题目:打印极值点下标
    用时:tomato *
    思路:经过观察,只需比较中间值和两侧数据的大小:比两侧都大或比两侧都小
    首尾数据比较特殊:只要不和旁边的数据相等则输出
    注意:打印的时候最后一个数字后面没有空格,因此我把所有即将被打印的索引存放到数组中,最后一起输出




*/


int main()
{
    int k,a[80];
    int x[80];
    int cur = 0;
    while (cin>>k)
    {
        for (int i=0;i<k;i++)
            cin>>a[i];

        for (int i=0;i<k;i++)
        {
            if (i==0 && a[i+1]!=a[i])
            {
                x[cur] = i;
                cur ++;
                continue;
            }
            if (i==k-1 && a[i]!=a[i-1])
            {
                x[cur] = i;
                cur ++;
                break;
            }
            if ((a[i-1] < a[i] && a[i+1] < a[i]) || (a[i-1] > a[i] && a[i+1] > a[i]))
            {
                x[cur] = i;
                cur ++;
            }
        }
        for (int i=0;i<cur-1;i++)
        {
            cout << x[i]<<' ';
        }
        cout <<x[cur-1];

    }



    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33846054/article/details/79514215
今日推荐