当前位置: 首页 > news >正文

函数题 01-复杂度3 二分查找【PAT】

文章目录

  • 题目
    • 函数接口定义
    • 裁判测试程序样例
    • 输入样例1
    • 输出样例1
    • 输入样例2
    • 输出样例2
  • 题解
    • 解题思路
    • 完整代码
    • AC代码

编程练习题目集目录

题目

  本题要求实现二分查找算法。

函数接口定义

Position BinarySearch( List L, ElementType X );

  其中 L i s t List List 结构定义如下:

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

   L L L是用户传入的一个线性表,其中 E l e m e n t T y p e ElementType ElementType 元素可以通过 > 、 = = 、 < >、==、< >==< 进行比较,并且题目保证传入的数据是递增有序的。函数 B i n a r y S e a r c h BinarySearch BinarySearch 要查找 X X X D a t a Data Data 中的位置,即数组下标(注意:元素从下标1开始存储)。找到则返回下标,否则返回一个特殊的失败标记 N o t F o u n d NotFound NotFound

裁判测试程序样例

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10
#define NotFound 0
typedef int ElementType;

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

List ReadInput(); /* 裁判实现,细节不表。元素从下标1开始存储 */
Position BinarySearch( List L, ElementType X );

int main()
{
    List L;
    ElementType X;
    Position P;

    L = ReadInput();
    scanf("%d", &X);
    P = BinarySearch( L, X );
    printf("%d\n", P);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1

5
12 31 55 89 101
31

输出样例1

2

输入样例2

3
26 78 233
31

输出样例2

0

题解

解题思路

  按照二分查找算法的思路对 L i s t List List 中的数据进行查找,具体查找思路可参考:查找算法,如果查找到目标数字,则直接返回其下标,如果没有找到则返回 N o t F o u n d NotFound NotFound (定义为 0 0 0);
  注:为了方便测试及代码完整性,对裁判实现的部分进行了模拟实现(有添加变量)。

完整代码

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10
#define NotFound 0
typedef int ElementType;

typedef int Position;
typedef struct LNode* List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last;                                  /* 保存线性表中最后一个元素的位置 */
};

int N;
List ReadInput();                                   /* 裁判实现,细节不表。元素从下标1开始存储 */
Position BinarySearch(List L, ElementType X);

int main(void)
{
    List L;
    Position P;
    ElementType X;
    
    printf("请输入要输入的数字个数:\n");
    scanf("%d", &N);
    L = ReadInput();
    printf("请输入整数要查找的数字:\n");
    scanf("%d", &X);
    P = BinarySearch(L, X);
    printf("%d\n", P);

    return 0;
}

List ReadInput() {                                      // 为了代码完整性,模拟实现 ReadInput() 函数
    List L = (List)malloc(sizeof(struct LNode));        // 分配内存
    if (L == NULL) {
        printf("内存分配失败!\n");
        exit(1);
    }
    L->Last = 0;                                        // 初始化为空表

    int count = 0;
    int value;
    printf("请输入 %d 个整数:\n", N);
    for (int i = 0; i < N; i++) {
        scanf("%d", &value);
        L->Last++;
        L->Data[L->Last] = value;
        count++;
    }

    if (count == MAXSIZE) {
        printf("警告:输入数据已达到最大容量 %d,剩余数据将被忽略。\n", MAXSIZE);
    }

    return L;
}

/* 你的代码将被嵌在这里 */
Position BinarySearch(List L, ElementType X) {
	int head = 1;
	int end = L->Last;
	while (head <= end) {
		int mid = (head + end) / 2;
		if (L->Data[mid] == X) {
			return mid;                         // 找到要查找的数字
		}
		else if (L->Data[mid] > X) {
            end = mid - 1;
		}
		else {
            head = mid + 1;
		}
	}
	return NotFound;                        // 未找到要查找到数字,返回0
}

AC代码

Position BinarySearch(List L, ElementType X) {
	int head = 1;
	int end = L->Last;
	while (head <= end) {
		int mid = (head + end) / 2;
		if (L->Data[mid] == X) {
			return mid;                         // 找到要查找的数字
		}
		else if (L->Data[mid] > X) {
            end = mid - 1;
		}
		else {
            head = mid + 1;
		}
	}
	return NotFound;                        // 未找到要查找到数字,返回0
}

相关文章:

  • 市盈率研究
  • Spring Boot集成EasyExcel
  • Python使用入门(二)
  • 侯捷 C++ 课程学习笔记:C++ 新标准11/14
  • 力扣练习之确定两个字符串是否接近
  • 【net2】mii,mdio,ncsi,bond,vlan,dns,ipv6
  • FPGA学习(三)——LED流水灯
  • 【redis】hash基本命令和内部编码
  • 串口全解析
  • 【Go每日一练】实现简单的控制台计算器
  • LVS + Keepalived 高可用集群
  • 《MySQL数据库从零搭建到高效管理|库的基本操作》
  • Android调试工具之ADB
  • QtDataVisualization使用
  • 100.Vue3 + OpenLayers:使用 marker-feature 添加 Marker
  • linux基本操作系统2
  • How to install a package in offline scenario in Ubuntu 24.04
  • bean的加载过程
  • WPF从初学者到专家:实战项目经验分享与总结
  • Gymnasium Taxi‐v3 环境 与 Q-learning 算法 —— 强化学习入门 I
  • 河南发布高温橙警:郑州、洛阳等地最高气温将达40℃以上
  • 台湾关闭最后的核电,岛内担忧“非核家园”缺电、涨电价困局难解
  • 从良渚到三星堆:一江水串起了5000年的文明对话
  • 沃尔玛上财季净利下滑12%:关税带来成本压力,新财季价格涨幅将高于去年
  • 一图读懂丨创新创业人才最高补贴500万元!临港新片区发布创客新政“十二条”
  • 董军同德国国防部长举行会谈