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

【c语言】统计一个数字在排序数组中出现的次数

// 题目:统计一个数字在排序数组中出现的次数。

//  比如:排序数组{1。2,3,3,3,3,4。5}和数字3,因为3出现了4次。因此输出4



有一种最简单的算法,遍历。可是有比它效率更高的


先看遍历:



#include <stdio.h>
#include <assert.h>

int num_time(int *arr, int len, int a)
{
	int i = 0;
	int count = 0;
	assert(arr != NULL);
	for (; i < len; ++i)
	{
		if (arr[i] == a)
			count++;
	}
	return count;
}

int main()
{
	int arr[] = { 1, 2, 3, 3, 3, 3, 4, 5 };
	int len = sizeof(arr) / sizeof(arr[0]);
	printf("%d\n", num_time(arr, len, 3));
	return 0;
}




另一种利用二分查找:



#include <stdio.h>

int GetFirstKey(int arr[], int left, int right, int len, int key)
{
	int mid;
	if (left > right)
	{
		return -1;
	}
	mid = left - (left - right) / 2;
	if (key == arr[mid])
	{
		if ((mid > 0 && arr[mid - 1] != key) || mid == 0)
		{
			return mid;
		}
		else
		{
			right = mid - 1;
		}
	}
	else if (arr[mid] < key)
	{
		left = mid + 1;
	}
	else
	{
		right = mid - 1;
	}
	return GetFirstKey(arr, left, right, len, key);
}

int GetLastKey(int arr[], int left, int right, int len, int key)
{
	int mid;
	if (left > right)
	{
		return -1;
	}
	mid = left - (left - right) / 2;
	if (key == arr[mid])
	{
		if ((mid < len - 1 && arr[mid + 1] != key || mid == len - 1))
		{
			return mid;
		}
		else
		{
			left = mid + 1;
		}
	}
	else if (arr[mid] < key)
	{
		left = mid + 1;
	}
	else
	{
		right = mid - 1;
	}
	return GetLastKey(arr, left, right, len, key);
}

int  main()
{
	int brr[] = { 1, 2, 3, 3, 3, 3, 4, 5};
	int len = sizeof(brr) / sizeof(brr[0]);
	int first = GetFirstKey(brr, 0, len - 1, len - 1, 3);
	int last = GetLastKey(brr, 0, len - 1, len - 1, 3);
	printf("%d\n", last - first + 1);
	return 0;
}





转载于:https://www.cnblogs.com/gavanwanggw/p/6847969.html

相关文章:

  • CentOS 下安装testlink
  • 历史记录
  • crack-jar游戏之CP9
  • SQLAlchemy -- Python的ORM驱动
  • console.log()注意事项。
  • NM常用网络命令
  • 点击手机号,跳转到手机打电话
  • BUS Matrix
  • 记前端小白入门15天
  • 大约 C++ 几个方面分析--overload, override, overwrite, rewrite
  • 字典类型的字符串转成字典
  • Aspose.Words 最新版发布【附下载】
  • go log包
  • C# 并行计算(Parallel 和 ParallelQuery)
  • Web客户端安全性最佳实践
  • [iOS]Core Data浅析一 -- 启用Core Data
  • Bytom交易说明(账户管理模式)
  • LeetCode18.四数之和 JavaScript
  • MD5加密原理解析及OC版原理实现
  • Node + FFmpeg 实现Canvas动画导出视频
  • NSTimer学习笔记
  • 对JS继承的一点思考
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 如何利用MongoDB打造TOP榜小程序
  • 试着探索高并发下的系统架构面貌
  • 优秀架构师必须掌握的架构思维
  • 【云吞铺子】性能抖动剖析(二)
  • LIGO、Virgo第三轮探测告捷,同时探测到一对黑洞合并产生的引力波事件 ...
  • Semaphore
  • 翻译 | The Principles of OOD 面向对象设计原则
  • ​iOS实时查看App运行日志
  • !$boo在php中什么意思,php前戏
  • $forceUpdate()函数
  • (C++20) consteval立即函数
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (分类)KNN算法- 参数调优
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • (三)uboot源码分析
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (转)nsfocus-绿盟科技笔试题目
  • (转)负载均衡,回话保持,cookie
  • *上位机的定义
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .L0CK3D来袭:如何保护您的数据免受致命攻击
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .net core webapi 大文件上传到wwwroot文件夹
  • .net core使用ef 6
  • .Net 知识杂记
  • .net/c# memcached 获取所有缓存键(keys)
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .NET正则基础之——正则委托
  • .vollhavhelp-V-XXXXXXXX勒索病毒的最新威胁:如何恢复您的数据?
  • /dev下添加设备节点的方法步骤(通过device_create)