DS静态查找之顺序查找

题目:
问题 A: DS静态查找之顺序查找
时间限制: 1 Sec  内存限制: 128 MB
提交: 335  解决: 301
[提交][状态][讨论版]
题目描述
给出一个队列和要查找的数值,找出数值在队列中的位置,队列位置从1开始

要求使用带哨兵的顺序查找算法

输入
第一行输入n,表示队列有n个数据
第二行输入n个数据,都是正整数,用空格隔开
第三行输入t,表示有t个要查找的数值
第四行起,输入t个数值,输入t行

输出
每行输出一个要查找的数值在队列的位置,如果查找不成功,输出字符串error

样例输入
8
33 66 22 88 11 27 44 55
3
22
11
99
样例输出
3
5
error
代码块:
#include <iostream>
using namespace std;

class SSTable
{
    int *elem;
    int length;
public:
    SSTable();
    ~SSTable();
    void InitSSTable();
    void Search_Seq();
};

SSTable::SSTable()
{
    cin>>length;
    elem = new int[length+1];
}

SSTable::~SSTable()
{
    delete []elem;
}

void SSTable::InitSSTable()
{
    elem[0] = 0;
    for(int i=1; i<=length; i++)
        cin>>elem[i];
}

void SSTable::Search_Seq()
{
    cin>>elem[0];
    int i = length;
    while(elem[i]!=elem[0])
        i--;
    if(i==0)
        cout<<"error"<<endl;
    else
        cout<<i<<endl;
}

int main(void)
{
    SSTable myTable;
    myTable.InitSSTable();
    int t;
    cin>>t;
    while(t--)
        myTable.Search_Seq();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值