LeetCode解题----C语言基本库函数的使用
在做题的时候,借助标准库中的函数,可以使我们更加专注于算法和题目本身。库函数的熟练使用,有助于我们聚焦于思路,快速解决问题。因此,很有必要熟练库函数了。以下库函数是本人在做题中经常遇到使用到的库函数,以下介绍按使用频率从大到小排序。
1.快速排序qsort
1.1 qsort函数原型
| 函数原型 | void qsort(voidbase, size_t num, size_t width, int( * compare)(const void,const void*)) |
| 库函数 | qsort |
| 包含头文件 | stdlib.h |
| ---- | ---- |
| 函数输入参数 | 参数描述 |
| void *base | 待排序数组首地址 |
| size_t num | 数组中带排序元素数量 |
| size_t width | 各元素的占用空间大小 |
| int( * compare)(const void,const void) | 指向函数的指针,用于却带排序的顺序,传入的是地址 |
| 函数原型 | compare( (void *) & elem1, (void *) & elem2) |
| compare函数的返回值 | 描述 |
| <0 | elem1将被排在ele2之前 |
| =0 | 位置不变 |
| >0 | elem1将被排在elem2之后 |
| exam: | |
| 从小到大排序 | int comp(const void *a, const void *b) {return *(int*)a-*(int*)b;} |
| 从大到小排序 | int comp(const void *a, const void *b) {return *(int*)b-*(int*)a;} |
1.2 qsort函数应用实例
1.2.1 对一维数组进行排序(从小到大排序):
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
return *(int *)a - *(int *)b; //从小到大排序
}
int main()
{
/* 数组输入准备 */
int *nums;
int numsSize;
scanf("%d", &numsSize);
nums = (int *)malloc(numsSize * sizeof(int)); // 动态申请数组
for (int i = 0; i < numsSize; i++)
{
scanf("%d", &nums[i]); // 数组元素赋值
printf("%d\t", nums[i]); //打印排序前数组
}
/* 数组调用qsort进行排序 */
qsort(nums, numsSize, sizeof(int), compare);
/* 打印排序后数组 */
printf("\nby sorted:\n");
for (int i = 0; i < numsSize; i++)
{
printf("%d\t", nums[i]);
}
return 0;
}
1.2.2 对二维数组进行排序(依据第一维对数组进行从小到大排序):
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
int *ap = *(int **)a;
int *bp = *(int **)b;
// 如果第一维相等,则依据第二维从小到大排序
if (ap[0] == bp[0])
return ap[1] - bp[1];
else
return ap[0] - bp[0]; // 第一维不相等,依据第一维从小到大排序
}
void printfnums(int **nums, int numsSize, int numsColSize)
{
for (int i = 0; i < numsSize; i++)
{
for (int j = 0; j < numsColSize; j++)
{
printf("%d ", nums[i][j]);
}
printf("\n");
}
}
int main()
{
int inputnums[][2] = {{1, 54}, {-4, 89}, {45, 6545}};
int numsColSize = sizeof(inputnums[0]) / sizeof(int);
int numsSize = sizeof(inputnums) / sizeof(inputnums[0]);
int **nums = (int **)malloc(numsSize * sizeof(int *));
for (int i = 0; i < numsSize; i++)
{
nums[i] = (int *)malloc(numsColSize * sizeof(int));
}
for (int i = 0; i < numsSize; i++)
{
for (int j = 0; j < numsColSize; j++)
{
nums[i][j] = inputnums[i][j];
}
}
printfnums(nums, numsSize, numsColSize);
qsort(nums, numsSize, sizeof(nums[0]), compare);
printfnums(nums, numsSize, numsColSize);
return 0;
}
1.2.3 对结构体进行排序:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct point
{
int x;
int y;
double distance;
};
int compare(const void *a, const void *b)
{
struct point *x = (struct point *)a;
struct point *y = (struct point *)b;
if (x->distance != y->distance)
{
return x->distance - y->distance;
}
else if (x->x != y->x)
{
return x->x - y->x;
}
else //if(x->y != x->y)
{
return x->y - y->y;
}
}
void pointsprintf(struct point *points, int numsSize)
{
for (int i = 0; i < numsSize; i++)
{
printf("distance :%0.2f x :%d y :%d\n", points[i].distance, points[i].x, points[i].y);
}
}
int main()
{
int nums[][2] = {{1, 3},
{-1, -1},
{56, 4},
{-1, -2}};
int numsSize = sizeof(nums) / sizeof(nums[0]);
int numsColSize = sizeof(nums[0]) / sizeof(int);
struct point points[numsSize];
//int *points = (int *)malloc(numsSize * sizeof(struct point));
for (int i = 0; i < numsSize; i++)
{
points[i].x = nums[i][0];
points[i].y = nums[i][1];
points[i].distance = sqrt(points[i].x * points[i].x + points[i].y * points[i].y);
}
pointsprintf(points, numsSize);
qsort(points, numsSize, sizeof(struct point), compare);
printf("\n");
pointsprintf(points, numsSize);
return 0;
}
1.2.4 对字符串进行排序:
compare 函数:
int compare(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}
调用:
qsort(products, productsSize, sizeof(char **), compare);
2. 整数与字符串的相互转换itoa/atoi
2.1 整数转字符串itoa
| 函数原型 | char* itoa(int value,char*string,int radix) |
| 库函数 | itoa |
| 包含头文件 | stdlib.h |
| ---- | ---- |
| 函数输入参数 | 参数描述 |
| int value | 要转换的整数 |
| char*string | 转换后的字符串 |
| int radix | 转换进制数,如2,8,10,16 进制等 |
| 返回值 | 指向转换后的字符串指针 |
#include <stdio.h>
#include <stdlib.h> // itoa 所在头文件
int main()
{
int num = 3;
char str[10];
// num:待转换的整数;str:转换后的字符串;10:转换进制数
itoa(num, str, 10);
printf("%s", str);
return 0;
}
2.2 字符串转整数atoi
| 函数原型 | int atoi(const char *str) |
| 库函数 | atoi |
| 包含头文件 | stdlib.h |
| ---- | ---- |
| 函数输入参数 | 参数描述 |
| const char *str | 待转换字符串 |
| 返回值 | 转换后的整数(int型) |
#include <stdio.h>
#include <stdlib.h> // atoi 所在库函数
int main()
{
char s[] = "22";
// s:待转换字符串;函数返回值:转换后的整数int
int num = atoi(s);
printf("%d", num);
return 0;
}
3. 字符串比较strcmp()/strncmp()
3.1 strcmp()
| 函数原型 | int strcmp(const char *str1, const char *str2) |
| 库函数 | strcmp |
| 包含头文件 | string.h |
| ---- | ---- |
| 函数输入参数 | 参数描述 |
| const char *str1 | 待比较字符串1的指针 |
| const char *str2 | 待比较字符串2的指针 |
| 返回值 | 如果返回值小于 0,则表示 str1 小于 str2。 如果返回值大于 0,则表示 str1 大于 str2。 如果返回值等于 0,则表示 str1 等于 str2。 |
#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "ab";
char s2[] = "aB";
if (strcmp(s1, s2) == 0)
{
printf("s1 == s2");
}
else if (strcmp(s1, s2) < 0)
{
printf("s1 < s2");
}
else if (strcmp(s1, s2) > 0)
{
printf("s1 > s2");
}
return 0;
}
3.2 strncmp()
| 函数原型 | int strncmp(const char *str1, const char *str2, size_t n) |
| 库函数 | strncmp |
| 包含头文件 | string.h |
| ---- | ---- |
| 函数输入参数 | 参数描述 |
| const char *str1 | 待比较字符串1的指针 |
| const char *str2 | 待比较字符串2的指针 |
| size_t n | 最多比较前 n 个字节 |
| 返回值 | 如果返回值小于 0,则表示 str1 小于 str2。 如果返回值大于 0,则表示 str1 大于 str2。 如果返回值等于 0,则表示 str1 等于 str2。 |
4. 字符串复制strcpy()/strncpy()
4.1 strcpy()
| 函数原型 | char *strcpy(char *dest, const char *src) |
| 库函数 | strcpy |
| 包含头文件 | string.h |
| ---- | ---- |
| 函数输入参数 | 参数描述 |
| char *dest | 指向用于存储复制内容的目标数组 |
| const char *src | 要复制的字符串 |
| 返回值 | 该函数返回一个指向最终的目标字符串 dest 的指针 |
#include <stdio.h>
#include <string.h>
int main()
{
char dest[20] = "ha ";
char src[] = "haha";
strcpy(dest, src);
printf("%s", dest);
return 0;
}
4.2 strncpy()
| 函数原型 | char *strncpy(char *dest, const char *src, size_t n) |
| 库函数 | strncpy |
| 包含头文件 | string.h |
| ---- | ---- |
| 函数输入参数 | 参数描述 |
| char *dest | 指向用于存储复制内容的目标数组 |
| const char *src | 要复制的字符串 |
| size_t n | 要从源中复制的字符数 |
| 返回值 | 该函数返回一个指向最终的目标字符串 dest 的指针 |
更多库函数的使用,可参考如下位置:
LeetCode解题----C语言基本库函数的使用的更多相关文章
- Leetcode解题思想总结篇:双指针
Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = f ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- C语言中库函数strstr的实现
在C语言中库函数strstr()函数表示在一个字符串str1中查找另一个字符串str2,如果查到则返回str2在str1中首次出现的位置,如果找不到则返回null. char* strstr(char ...
- 通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制
通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制 前言说明 本篇为网易云课堂Linux内核分析课程的第四周作业,我将通过调用C语言的库函数与在C代码中 ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- C语言之库函数的模拟与使用
C语言之库函数的模拟与使用 在我们学习C语言的过程中,难免会遇到这样的一种情况: 我们通常实现一个功能的时候,费尽心血的写出来,却有着满满的错,这时却有人来告诉你说:这个功能可以用相应的库函数来实现. ...
- LeetCode解题记录(贪心算法)(二)
1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等 ...
- 问题解决:补充安装c语言的库函数和系统调用man手册
问题解决:补充安装c语言的库函数和系统调用man手册 今日份麻麻~上课时大家的Ubuntu都可以通过man查到关于stat的库函数,但是我的Kali查出来是这样: 询问老师之后得知需要去安装相 ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- LeetCode解题中位运算的运用
位运算是我最近才开始重视的东西,因为在LeetCode上面刷题的时候发现很多题目使用位运算会快很多.位运算的使用包含着许多技巧(详细可以参考http://blog.csdn.net/zmazon/ar ...
随机推荐
- P8842 [传智杯 #4 初赛] 小卡与质数2
传送门 变态数学题(主考位运算与素数筛). 读完题看起来有点难做,因为质数的出现是根本没有可以使用的规律.暴力的话也很好想,枚举 $y$.但是肯定会超时.我们也可以换个方向枚举.对,筛出素数,再返过去 ...
- MyBatisPlus逆向工程
MyBatisPlus逆向工程 一.创建Springboot工程 二.引入pom依赖 <?xml version="1.0" encoding="UTF-8&quo ...
- Vertx 接入Redis (八)
项目github地址:https://github.com/fotocj007/VertxWebApi web服务器经典的mysql+redis模式,这里介绍redis的接入. 一:导入gradle ...
- 支付宝小程序textarea字数统计踩坑
前情 uni-app是我比较喜欢的跨平台框架,它能开发小程序/H5/APP(安卓/iOS),重要的是对前端开发友好,自带的IDE让开发体验也挺棒的,公司项目就是发布多端的项目,所以主推的是uni-ap ...
- AI应用实战课学习总结(1)必备AI基础理论
大家好,我是Edison. 由于公司的愿景逐渐调整为ONE Tech Company,公司的IT战略也逐渐地朝着Data & AI Driven发展,因此近半年来我一直在学习大模型相关的东西, ...
- Ubuntu云服务器上部署发布Vite项目
1 拷贝代码 一般来说是Windows环境下开发,Ubuntu环境下部署.因此首先要考虑的问题是如何将Vite项目的源代码拷贝到云服务器上面去.最简单的就是使用像MobaXterm这样的远程连接工具, ...
- docker入门基操~~~
# Docker基本使用方式 - [Window 使用 Docker 创建lnmp环境 ](#introduction) - [常用docker命令 ](#command) ### [安装详情](ht ...
- 使用systemd 监控服务并实现故障自动重启
一.为什么需要自动重启? 在生产环境中,服务可能因内存溢出.资源竞争.外部依赖中断等问题意外崩溃.手动恢复效率低下,而 systemd 的自动重启机制可在秒级内恢复服务,显著提升系统可用性. ⚙️ 二 ...
- LaTeX编辑数学公式基本语法
LaTeX编辑数学公式基本语法 引用自: [1]https://blog.csdn.net/qq_33532713/article/details/108602463 [2]https://www.c ...
- Modbus网关ZLAN5443D 在锂电池干燥箱的应用
在锂离子电池生产过程中,将正负极片辊压绕卷再放入电池盒之后,须对锂电池电芯极组进行烘烤干燥.相信大家也了解水分对锂电池的性能影响是很大的,需要注液前在装配车间将锂离子电池电芯内部的水分去除,以免影响锂 ...