纯小白蓝桥杯备赛笔记--DAY5(竞赛常用库函数)

2024-03-31 23:12

本文主要是介绍纯小白蓝桥杯备赛笔记--DAY5(竞赛常用库函数),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 大小写转换
      • islower和isupper:检查一个字符是否是小写或者大写。
      • Tolower和toupper函数:
      • ASCII码:
    • 二分查找
      • 二分查找的前提:库函数只能对数组进行二分查找,且数组中的元素是单调的。
      • binary_search函数:
      • lower_bound和upper_bound函数
    • 最值查找
      • Min和max函数:
      • min_element和max_element函数:
    • 排序
      • sort简介:
      • 自定义比较函数:
    • 全排列
      • next_permutation函数
      • prev_permutation()函数:
    • 其他库函数
      • memset()函数:
      • swap()函数:
      • reverse()函数:
      • unique()函数

大小写转换

islower和isupper:检查一个字符是否是小写或者大写。

  • islower:检查小写。
  • isupper:检查大写。
  • 函数的返回值为bool类型。
  • 头文件:或者是<bits/stdc++.h>

Tolower和toupper函数:

  • tolower(char ch)将ch转换为小写字母,大写不转换。
  • toupper同理。

ASCII码:

  • 大写字母的范围:65~90
  • 小写字母的范围:97~122
    大写转小写:
    ch1=ch+32;//不推荐
    ch1=ch-‘A’+‘a’;//减去大写的那一列,从小写的那一列开始
    实现:小写转大写
#include<bits/stdc++.h>
using namespace std;
char coverch(char ch)
{if(islower(ch))ch=toupper(ch);else if(isupper(ch))ch=tolower(ch);return ch;
}
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);string s;getline(cin,s);for(auto &i:s){i=coverch(i);}cout<<s<<"\n";        
}

二分查找

二分查找的前提:库函数只能对数组进行二分查找,且数组中的元素是单调的。

  • 一般为单调不减,当然如果是单调不增也可以(需要修改比较函数sort)

binary_search函数:

  • 用法:已排序的序列中查找特定元素。
  • 通过二分查找算法来确定列中是否存在目标元素。
  • 函数返回值是bool类型。
  • 获取找到元素的位置使用lower_bound函数或upper_bound函数。
#include<bits/stdc++.h>
int main()
{using namespace std;ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>numbers={1,3,5,7,9};int target=5;bool found=binary_search(numbers.begin(),numbers.end(),target);if(found)cout<<target<<" "<<"is found"<<endl;elsecout<<target<<"is not found"<<endl;return 0;} 

lower_bound和upper_bound函数

  • lower_bound(start,end,target)返回地址[start,end)中第一个大于等于x的元素的地址。(注:下标=地址-首地址)
  • upper_bound(start,end,target)返回地址[start,end)中第一个大于 x的元素的地址。(注意是左闭右开的区间)
  • 如果不存在则返回最后一个元素的下一个位置,在vector中即end()。
  • 实现
#include<bits/stdc++.h>
using namespace std;
int main()
{vector<int> s={5,1,7,3,10,18,9};sort(s.begin(),s.end());for(auto &i:s)cout<<i<<' ';cout<<"\n";//找到数组中第一个大于等于8的元素的地址cout<<(lower_bound(s.begin(),s.end(),8)-s.begin())<<"\n";return 0; 
}
  • 例题:蓝桥1389二分查找数组
#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);int data[200];for(int i = 0 ; i < 200 ; i ++)data[i] = 4 * i + 6;int target;cin>>target;cout<<lower_bound(data,data+200,target)-data<<"\n";return 0;
}

注:不是容器(例如:vector和list)对象的不能用begin()函数。修改如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>data(200);for(int i = 0 ; i < 200 ; i ++)data[i] = 4 * i + 6;int target;cin>>target;cout<<lower_bound(data.begin(),data.end(),target)-data.begin()<<"\n";return 0;
}

最值查找

Min和max函数:

  • 时间复杂度为O(1),传入参数为数组(用{})的时候时间复杂度为O(n),n为数组大小。

min_element和max_element函数:

  • min_element(start,end)返回地址[start,end)中的最小的那个值的地址(迭代器),传入参数为两个地址或迭代器。
  • Max_element(start,end)返回地址[start,end)中的最大的那个值的地址(迭代器),传入参数为两个地址或迭代器。
  • 时间复杂度为O(n),n为数组大小
  • 返回值是具体的数字,而不是一个地址
#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>v={5,1,3,9,11};cout<<*min_element(v.begin(),v.end())<<"\n";return 0;
}

注:这段代码中的星号(*)表示解引用,即通过地址(迭代器)得到值。

  • nth_element()函数
    • 格式:nth_element(start,k,end)
    • 用途,找出第k大个元素,则该元素的位置属于正确位置,其他元素的位置虽然是任意的,但是前面的都比它小,后面的都比它大。
    • 返回值为void()
    • 时间复杂度为O(n)
#include<bits/stdc++.h>
using namespace std;
int main()
{vector<int>v={5,1,7,3,10,18,9};nth_element(v.begin(),v.begin()+3,v.end());//返回值为空,说明这条语句不能输出for(auto &i:v)cout<<i<<"\n";return 0; 
}
  • 例题:497求最高分,最低分和平均分。(两种方法)
#include<bits/stdc++.h>
using namespace std;
using ll=long long;//数组记得开long long
const int N=10001;
int a[N];
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);int n;cin>>n;ll sum=0;for(int i=1;i<=n;i++){cin>>a[i];sum+=a[i];        }cout<<*max_element(a+1,a+n+1)<<"\n";//定义的是数组时使用数组名,定义的是容器时使用begin函数 cout<<*min_element(a+1,a+n+1)<<"\n";cout<<fixed<<setprecision(2)<<1.0*sum/n<<"\n";return 0;          
}
#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);int n;int s[10001];int maxa=0;int mina=100;double sum=0;double c=0;cin>>n;for(int i=0;i<n;i++){cin>>s[i];maxa=max(maxa,s[i]);mina=min(mina,s[i]);sum+=s[i];} c=sum/double(n);cout<<maxa<<endl;cout<<mina<<endl;printf("%.2lf",c);return 0;
}

注:c++如何保留小数:

#include <iostream>
#include <iomanip>int main() {double num = 3.14159;std::cout << std::fixed << std::setprecision(2) << num << std::endl;return 0;
}

使用std::fixed和std::setprecision(2)设置输出保留两位小数。

排序

sort简介:

  • sort函数包含在头文件中。
  • 在使用前需要#include或者使用万能头文件。
  • 用途:指定范围内的元素进行排序。
  • sort算法使用的是快速排序(QuickSort)或者类似快速排序的改进算法,时间复杂度为O(nlogn)。
  • 用法:sort(起始地址,结束地址的下一位,*比较函数)
 #include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);int a[100];int n;cin>>n;for(int i=1;i<=n;i++)cin>>a[i];//对数组进行排序sort(a+1,a+n+1);//输出for(int i=1;i<=n;i++)cout<<a[i]<<' ';return 0; } 
#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>a={5,1,3,9,11};//对数组进行排序sort(a.begin(),a.end());//输出for(auto i:a)cout<<i<<' ';return 0; } 

自定义比较函数:

  • 在sort函数末尾传入第三个表达式,这个表达式可以是函数也可以是lambda表达式。
    • 函数:
#include<bits/stdc++.h>
using namespace std;
bool cmp(const int &u,const int &v)
{return u>v;
}
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>a={5,1,3,9,11};//对数组进行排序sort(a.begin(),a.end(),cmp);//输出for(int i=0;i<a.size();i++)cout<<a[i]<<' ';return 0; } 

lambda表达式:(匿名函数,一般用在只在程序中用一下,其他地方用不到的情况)

#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>a={5,1,3,9,11};//对数组进行排序sort(a.begin(),a.end(),[](const int &u,const int &v)//方括号内也可以放&表示引用 {return u>v;});//输出for(int i=0;i<a.size();i++)cout<<a[i]<<' ';return 0; } 
  • 也可以用在结构体中,其中函数的参数只有一个。
 struct Node{int u,v;bool operator < (const Node &m)const//这里可以把perator < 整体看做一个函数名 {return u==m.u?v<m.v:u<m.u;//以u为第一关键字,v为第二关键字 }} 
  • 例题:1265:对一个数组分别实现由小到大和由大到小输出
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+3;
int a[N];
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);int n;cin>>n;for(int i=1;i<=n;++i)cin>>a[i];sort(a+1,a+n+1);for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];//到第n个的时候输出一个换行for(int i=n;i>=1;--i)cout<<a[i]<<" \n"[i==1];//当i等于1的时候跟上一个回车return 0;
}

注:12,13行后面的字符判断"\n"之前有一个空格一定要记得打上。

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+3;
int a[N];
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);int n;cin>>n;for(int i=1;i<=n;++i)cin>>a[i];sort(a+1,a+n+1);for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];//到第n个的时候输出一个换行//当i等于1的时候跟上一个回车sort(a+1,a+n+1,[](const int &u,const int &v){return u>v;});for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];return 0;
}
  • 第二种方法
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+3;
int a[N];
bool cmp(const int &u,const int &v)
{return u>v;
}
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);int n;cin>>n;for(int i=1;i<=n;++i)cin>>a[i];sort(a+1,a+n+1);for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];//到第n个的时候输出一个换行//当i等于1的时候跟上一个回车sort(a+1,a+n+1,cmp);for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];return 0;
}

全排列

next_permutation函数

  • 该函数用于生成当前序列的下一个排列。它按照字典序对序列进行重新排序,如果存在下一个序列,则将当前序列更改为下一个排列,并返回true,如果当前序列已经是最后一个排列,则将序列更改为第一个排列,并返回false。
  • 如果想要输出全部的排列就要求定义的数组是最小的。
#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>nums={1,2,3}; cout<<"初始的数组:";for(int num:nums){cout<<num<<" ";}cout<<endl;//生成下一个排列while(next_permutation(nums.begin(),nums.end())) {cout<<"下一个排列是:";for(int num:nums){cout<<num<<" ";} cout<<endl;}return 0;
}

prev_permutation()函数:

  • 生成当前序列的上一个排列。同样,要想生成全排列要求第一个是最大的数组。
#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>nums={3,2,1}; cout<<"初始的数组:";for(int num:nums){cout<<num<<" ";}cout<<endl;//生成下一个排列while(prev_permutation(nums.begin(),nums.end())) {cout<<"下一个排列是:";for(int num:nums){cout<<num<<" ";} cout<<endl;}return 0;
}

补充:全排列一般使用搜索(dfs)
应用:

#include<bits/stdc++.h>
using namespace std;
int a[10];
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);for(int i=1;i<=4;++i)a[i]=i;bool tag=true;while(tag){for(int i=1;i<=4;++i)cout<<a[i]<<' ';cout<<'\n';tag=next_permutation(a+1,a+1+4);}return 0;
}

注:next_permutation 函数用于生成给定范围内的下一个排列。如果成功生成了下一个排列,则返回 true,并将数组 a 更新为新的排列;否则,如果没有更多的排列可用,则返回 false。由此tag实现对循环的控制。

其他库函数

memset()函数:

  • 用途:设置内存块值。
  • 原型定义在头文件中。
  • 函数声明:void* memset(void* ptr,int value,size_t num);分别是:指针、值、重置的大小。
  • 详解:
    • ptr:指向要设置值的内存块的地址(数组一般是数组名)
    • value:要设置的值,通常是一个整数。
    • num:要设置的字节数。
    • 就是说,将ptr指向的内存块的前num个字节设置为value的值。它返回一个指向ptr的指针。
    • memset()通常用于初始化内存块,将其设置为特定的值。
  • 注:memset()函数对于非字符类型的数组可能会产生未定义行为(每一个字节赋值为1)
#include<bits/stdc++.h>
using namespace std;
int a[10];
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);int a[5];memset(a,1,sizeof(a));//设置成0和-1没有影响//-1的补码是全1的。for(int i=0;i<5;++i)cout<<bitset<32>(a[i])<<'\n';//产看a[i]的二进制 return 0;
}

swap()函数:

  • 用法:swap(T &a,T &b);函数接受两个参数进行交换。

reverse()函数:

  • 用于反转容器中元素顺序的函数。
  • 原型定义在头文件中,函数的声明如下:
  • reverse(first,last)函数接受两个参数:将[first,last)范围内的元素顺序进行反转。
  • reverse可翻转各种类型的容器,如数组、向量、链表等。
#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>vec={1,2,3,4,5};reverse(vec.begin(),vec.end()) ;for(int num:vec)cout<<num<<" ";cout<<endl;return 0;
}

unique()函数

  • unique(first,last)将[first,last)范围内的相邻重复元素去除,并返回一个指向去重后范围的尾后迭代器。去重后的范围中只保留第一个出现的元素,后续重复的元素都被移除。
  • 时间复杂度为O(n),但是使用之前一般要排序,此时时间复杂度为sort(nlogn)。
#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);vector<int>vec={1,1,2,2,3,3,4,4,5,5};auto it=unique(vec.begin(),vec.end());//unique 函数会将相邻的重复元素移动到向量的末尾,并返回一个指向去重后最后一个元素的下一个位置的迭代器。vec.erase(it,vec.end());//vec.erase(it,vec.end()); 这行代码的作用是删除从迭代器 it 开始到向量 vec 末尾的所有元素。for(int num:vec){cout<<num<<" ";}cout<<endl;return 0;
}

//定义数组的形式:

#include<bits/stdc++.h>
using namespace std;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);int vec[]={1,1,2,2,3,3,4,4,5,5};int n=unique(vec,vec+10)-vec;//减去a最后得到的是下标for(int i=0;i<n;i++)cout<<vec[i]<<' '; return 0;
}

未完待续,大家一起加油啊!!!

这篇关于纯小白蓝桥杯备赛笔记--DAY5(竞赛常用库函数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/865607

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

常用的jdk下载地址

jdk下载地址 安装方式可以看之前的博客: mac安装jdk oracle 版本:https://www.oracle.com/java/technologies/downloads/ Eclipse Temurin版本:https://adoptium.net/zh-CN/temurin/releases/ 阿里版本: github:https://github.com/

30常用 Maven 命令

Maven 是一个强大的项目管理和构建工具,它广泛用于 Java 项目的依赖管理、构建流程和插件集成。Maven 的命令行工具提供了大量的命令来帮助开发人员管理项目的生命周期、依赖和插件。以下是 常用 Maven 命令的使用场景及其详细解释。 1. mvn clean 使用场景:清理项目的生成目录,通常用于删除项目中自动生成的文件(如 target/ 目录)。共性规律:清理操作

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

论文阅读笔记: Segment Anything

文章目录 Segment Anything摘要引言任务模型数据引擎数据集负责任的人工智能 Segment Anything Model图像编码器提示编码器mask解码器解决歧义损失和训练 Segment Anything 论文地址: https://arxiv.org/abs/2304.02643 代码地址:https://github.com/facebookresear

019、JOptionPane类的常用静态方法详解

目录 JOptionPane类的常用静态方法详解 1. showInputDialog()方法 1.1基本用法 1.2带有默认值的输入框 1.3带有选项的输入对话框 1.4自定义图标的输入对话框 2. showConfirmDialog()方法 2.1基本用法 2.2自定义按钮和图标 2.3带有自定义组件的确认对话框 3. showMessageDialog()方法 3.1

数学建模笔记—— 非线性规划

数学建模笔记—— 非线性规划 非线性规划1. 模型原理1.1 非线性规划的标准型1.2 非线性规划求解的Matlab函数 2. 典型例题3. matlab代码求解3.1 例1 一个简单示例3.2 例2 选址问题1. 第一问 线性规划2. 第二问 非线性规划 非线性规划 非线性规划是一种求解目标函数或约束条件中有一个或几个非线性函数的最优化问题的方法。运筹学的一个重要分支。2