顺序查找:c语言

参考视频:

顺序查找和平均查找长度ASL_哔哩哔哩_bilibili

6-21 顺序查找

分数 25

作者 杨嫘

单位 桂林学院

在一个顺序表中找x,输出该数最先出现的位置,没有找到则提示查找失败。

函数接口定义:

复制代码
int searchSq(SqList L,ElemType x); 

其中 Lx 都是用户传入的参数。 L是顺序表; x 是要查找的元素值。函数须返回该数最先出现的位置,没有找到则返回-1。

裁判测试程序样例:

复制代码
#include <stdio.h>
#define MAXSIZE 1000
typedef int ElemType;
typedef struct SqList{
    ElemType data[MAXSIZE];
    int len;
}SqList;
void createSq(SqList *L);  //输入函数,具体实现略 
void printSq(SqList L);   //输出函数,具体实现略 
int searchSq(SqList L,ElemType x); 
int main() 
{
    SqList L;
    createSq(&L);
    int x,n=0;
    scanf("%d",&x);
    n=searchSq(L,x);
    if(n==-1)
        printf("sorry,can't find it.");
    else
        printf("The first place it appears is %d. ",n);
}
/* 请在这里填写答案 */

输入样例1:

复制代码
6
0 2 4 5 8 9
4

输出样例1:

复制代码
The first place it appears is 2.

输入样例2:

复制代码
10
8 9 5 0 2 4 6 4 11 4
4

输出样例2:

复制代码
The first place it appears is 5.

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

C (gcc)

答案

复制代码
int searchSq(SqList L,ElemType x){

for(int i=0;i<L.len;i++){

if(x==L.data[i]){

return i;

}

}

return -1;

}
相关推荐
wuyk5556 小时前
1.栈:后进先出的线性数据结构
c语言·数据结构·stm32·单片机
测试_AI_一辰6 小时前
AI Agent 评测最隐蔽的坑-记忆
人工智能·算法·ai·自动化·ai编程
躺不平的理查德6 小时前
Windows C++ 第三方库使用流程备忘录--OpenCV
开发语言·c++
lucas_AI6 小时前
给YOLO检测器插 LoRA,'插对地方'比'插什么'更要命
人工智能·算法
菜冻鱼6 小时前
Python-sklearn-评估指标
开发语言·人工智能·python·机器学习·numpy·pandas·sklearn
你挚爱的强哥6 小时前
【exportExcel】单纯靠js不用任何其他第三方插件导出xls格式文件
开发语言·javascript·ecmascript
LuminousCPP6 小时前
数据结构-时间与复杂度|时间/空间复杂度 + 两道力扣练习 + 二分查找复盘
c语言·数据结构·经验分享·算法·leetcode
Nil2086 小时前
leetcode 三数之和
数据结构·算法·leetcode
Hello_Damon_Nikola7 小时前
AC7911从入门到精通
开发语言·嵌入式硬件·物联网·php
worxfr7 小时前
Go 并发控制:从 Channel 方向约束到实战模式
开发语言·后端·golang