机试准备第10天
首先学习二分搜索法。使用二分查找需要先排序。第一题是查找,现学现卖。
//二分查找
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int main(){int n;scanf("%d", &n);vector<int> a(n);for(int i = 0; i < n;i++){scanf("%d", &a[i]);}sort(a.begin(), a.end());int m;scanf("%d", &m);int b;for(int i = 0; i < m;i++){scanf("%d", &b);int left = 0;int right = n-1;while(left<=right){int mid = (left+right)/2;if(b == a[mid]) {printf("Yes\n");break;}else if(b < a[mid]) right = mid -1;else left = mid+1;}if(left > right) printf("NO\n");}}
使用map优化二分查找,把所有查找的数据放入map中,用map的键查找值,基于红黑树的map性能较好,但是会占用空间,如果map的时间性能依然不能满足,则选择unordered_map优化时间,unordered_map基于哈希查找,代价是更多的额外空间。
#include <vector>
#include <stdio.h>
#include <map>
#include <algorithm>
using namespace std;
int main(){int n;scanf("%d" , &n);map<int, int> map1;for(int i = 0; i < n;i++){int mid;scanf("%d", &mid);map1.insert({mid, i});}int m;scanf("%d", &m);for(int i = 0; i < m;i++){int b;scanf("%d", &b);if(map1.find(b) == map1.end()) printf("NO\n");else printf("YES\n");}
}
第二题是找位置,本题亮点在于使用map<char, vector<int>> map1,用vector作为键值对中的值,记录各个字符出现的下标位置。
#include <stdio.h>
#include <map>
#include <vector>
using namespace std;
int main(){char str[100];scanf("%s", str);map<char, vector<int>> timesMap;//记录每个字符的位置与次数vector<char> charseq;//记录每个字符出现的先后顺序string cstr = str;for(int i = 0; str[i] != '\0';i++){timesMap[str[i]].push_back(i);//如果是第一次出现if(timesMap[str[i]].size() == 1){charseq.push_back(str[i]);}}for(int i = 0; i < charseq.size();i++){if(timesMap[charseq[i]].size()>=2){printf("%c:%d", charseq[i], timesMap[charseq[i]][0]);for(int j = 1;j < timesMap[charseq[i]].size();j++){printf(",%c:%d", charseq[i], timesMap[charseq[i]][j]);}printf("\n");}}}
第三题是找最小数,经典的结构体sort排序。
#include <future>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
struct Num {int val1;int val2;
};
bool cmp(Num left, Num right) {if (left.val1 < right.val1) return true;else if (left.val1 == right.val1 && left.val2 < right.val2) return true;else return false;
}
int main() {int n;while (scanf("%d", &n) != EOF) {vector<Num> vec1(n);for (int i = 0; i < n; i++) {scanf("%d%d", &vec1[i].val1, &vec1[i].val2);}sort(vec1.begin(), vec1.end(), cmp);printf("%d %d\n", vec1[0].val1, vec1[0].val2);}
}
第四题是打印极值点下标,主要注意两侧特殊情况的判定。
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int main(){int n;scanf("%d", &n);vector<int> vec(n);for(int i = 0; i < n;i++){scanf("%d", &vec[i]);//读入数组}vector<int> res;//结果数组if(n==1) printf("0\n");else {if(vec[0]!=vec[1]) res.push_back(0);for(int i = 1; i <=(n-2);i++){if((vec[i]<vec[i-1]&&vec[i]<vec[i+1])||(vec[i]>vec[i-1]&&vec[i]>vec[i+1]))res.push_back(i);}if(vec[n-2]!=vec[n-1]) res.push_back(n-1);sort(res.begin(), res.end());for(int i = 0; i<res.size();i++){printf("%d ", res[i]);}printf("\n");}}
第五题是差分计数,又是华东师范的恶心题。
#include <unordered_map>
#include <stdio.h>
#include <vector>
using namespace std;
int main(){int n,x;scanf("%d%d", &n,&x);vector<int> vec(n);for(int i =0;i < n;i++){scanf("%d", &vec[i]);}unordered_map<int, long long> diffCount;for(int j = 0; j < n;j++){++diffCount[vec[j] + x];}long long count = 0;for(int i = 0; i < n; i++){count += diffCount[vec[i]];}printf("%lld\n", count);
}
下面进行字符串的学习,C风格的字符串不能支持赋值(=),比较大小(><),和判断相等(==),因此在使用方面有麻烦。C++风格字符串支持比较运算符,类似于vector<char>,同样支持push_back等操作,缺点是不能printf与scanf。
#include <stdio.h>
#include <string>
using namespace std;
int main(){string str1 = "hello";string str2 = "world!";string str3; str3 = "hello";//string 支持 = 赋值//string支持==判断内容是否相同bool issame = false;issame =(str1==str3);//if(issame == true) printf("OK");//string支持 + 连接操作str3 = str1+str2;//printf("%s", str3.c_str());//string支持使用< <=比较大小,利用字典序//if(str2>str1) printf("OK");//string 非常像vector<char>string str4 = "abcdefg";char ch = str4[0];str4.push_back('F');//printf("%s", str4.c_str());string::iterator it;
// for(it = str4.begin();it!=str4.end();it++){
// printf("*it = %c\n", *it);
// }it = str4.begin();str4.insert(it, 'A');it = str4.end()-1;str4.erase(it);
// for(it = str4.begin();it!=str4.end();it++){
// printf("*it = %c\n", *it);
// }//string 对比vector 拓展了insert和erase的用法//string使用整数下标,插入删除多个字符str4.insert(0, "xyz");//整数下标 字符串常量str4.erase(0, 3);//两个整数下标,删除范围[0,3)//获取字串string str5;str5 = str4.substr(0, 3);//从0开始 长度为3//字符串匹配string str6 = "howdoyoudo";int pos = str6.find("dd", 0);printf("%d", pos);if(pos == string::npos) printf("找不到");//string与数值相互转换,to_string,Sto系列函数 Stoi , Stol...int i= 1234;string str7 = to_string(i);float j = 2.41;str7 = to_string(j);string str8 = "3.14159";j = stof(str8);//输入使用字符数组转化,输出使用string.c_str()
}
第六题是单词个数统计,没啥说的,简单模拟。
#include <stdio.h>
#include <string>
using namespace std;
int main(){char str1[1000];//单词数组int word = 0;int res[26] = {0};int letternum=0;while(scanf("%s", str1)!=EOF){word++;string str2 = str1;for(int i = 0; i<str2.size();i++){if(str2[i]>='a'&&str2[i]<='z') res[str2[i]-'a']++;else if(str2[i]>='A'&&str2[i]<='Z') res[str2[i]-'A']++;letternum++;}}int max = 0;printf("%d\n", letternum);printf("%d\n", word);for(int i = 0;i < 26;i++){if(res[i]>max) max = res[i];}for(int i = 0; i < 26;i++){if(res[i]==max) printf("%c ",'a'+i);}printf("\n");printf("%d", max);
}
第七题是字母统计,主要是搞清楚一行字符串的输入方法,getline(cin, str)。多行输入为while(getline(cin,str))。
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main(){string str;while(getline(cin, str)){int res[26] = {0};for(int i = 0; i < str.size();i++){if(str[i] >= 'A' && str[i] <= 'Z')res[str[i] - 'A']++;}for(int i = 0;i < 26;i++){printf("%c:%d\n", 'A'+i, res[i]);}}}
第八题是替换单词,取巧成功哈哈哈。
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
int main(){char word[100];vector<string> res;while(scanf("%s", word)!=EOF){string str = word;res.push_back(str);}string before = res[res.size() - 2];string after = res[res.size() - 1];res.pop_back();res.pop_back();for(int i = 0; i < res.size();i++){if(res[i] == before) res[i] = after;}for(int i = 0; i < res.size();i++){printf("%s ", res[i].c_str());}}
相关文章:
机试准备第10天
首先学习二分搜索法。使用二分查找需要先排序。第一题是查找,现学现卖。 //二分查找 #include <stdio.h> #include <vector> #include <algorithm> using namespace std; int main(){int n;scanf("%d", &n);vector<int> a(n…...
Apache ECharts介绍(基于JavaScript开发的开源数据可视化库,用于创建交互式图表)
文章目录 Apache ECharts 介绍功能概览多种图表类型- **基础类型**:折线图、柱状图、饼图、散点图。- **高级类型**:雷达图、热力图、桑基图、K线图。- **地理可视化**:支持地图(如中国、世界)和地理坐标系。- **3D支持…...

最新版本TOMCAT+IntelliJ IDEA+MAVEN项目创建(JAVAWEB)
前期所需: 1.apache-tomcat-10.1.18-windows-x64(tomcat 10.1.8版本或者差不多新的版本都可以) 2.IntelliJ idea 24年版本 或更高版本 3.已经配置好MAVEN了(一定先配置MAVEN再搞TOMCAT会事半功倍很多) 如果有没配置…...
Linux - 进程通信
一、管道 管道是一种进程间通信(IPC)机制,用于在进程之间传递数据。它的本质是操作系统内核维护的一个内存缓冲区,配合文件描述符进行数据的读写。尽管管道的核心是内存缓冲区,但操作系统通过对管道的实现,…...

使用 Arduino 的 WiFi 控制机器人
使用 Arduino 的 WiFi 控制机器人 这次我们将使用 Arduino 和 Blynk 应用程序制作一个 Wi-Fi 控制的机器人。这款基于 Arduino 的机器人可以使用任何支持 Wi-Fi 的 Android 智能手机进行无线控制。 为了演示 Wi-Fi 控制机器人,我们使用了一个名为“Blynk”的 Andr…...

网络安全等级保护2.0 vs GDPR vs NIST 2.0:全方位对比解析
在网络安全日益重要的今天,各国纷纷出台相关政策法规,以加强信息安全保护。本文将对比我国网络安全等级保护2.0、欧盟的GDPR以及美国的NIST 2.0,分析它们各自的特点及差异。 网络安全等级保护2.0 网络安全等级保护2.0是我国信息安全领域的一…...
verb words
纠正correct remedy 修正modify 协商 confer 磋商/谈判 negotiate 通知notice notify *宣布announce 声明declare 宣告 declare *颁布 promulgate /introduce 协调coordinate 评估evaluate assess 撤离evacuate *规定stipulate 参与participate, 涉及refer…...

unity console日志双击响应事件扩展
1 对于项目中一些比较长的日志,比如前后端交互协议具体数据等,这些日志内容可能会比较长,在unity控制面板上查看不是十分方便,我们可以对双击事件进行扩展,将日志保存到一个文本中,然后用系统默认的文本查看…...
维度建模维度表技术基础解析(以电商场景为例)
维度建模维度表技术基础解析(以电商场景为例) 维度表是维度建模的核心组成部分,其设计直接影响数据仓库的查询效率、分析灵活性和业务价值。本文将从维度表的定义、结构、设计方法及典型技术要点展开,结合电商场景案例,深入解析其技术基础。 1. 维度表的定义与作用 定义…...

Leetcode 264-丑数/LCR 168/剑指 Offer 49
题目描述 我们把只包含质因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。 示例: 说明: 1 是丑数。 n 不超过1690。 题解 动态规划法 根据题意,每个丑数都可以由其他较小的丑数通过乘以 2 或 3 或 5 得到…...
阿里云MaxCompute面试题汇总及参考答案
目录 简述 MaxCompute 的核心功能及适用场景,与传统数据仓库的区别 解释 MaxCompute 分层架构设计原则,与传统数仓分层有何异同 MaxCompute 的存储架构如何实现高可用与扩展性 解析伏羲(Fuxi)分布式调度系统工作原理 盘古(Pangu)分布式存储系统数据分片策略 计算与存…...
笔记:Directory.Build.targets和Directory.Build.props的区别
一、目的:分享Directory.Build.targets和Directory.Build.props的区别 Directory.Build.targets 和 Directory.Build.props 是 MSBuild 的两个功能,用于在特定目录及其子目录中的所有项目中应用共享的构建设置。它们的主要区别在于应用的时机和用途。 二…...
istio入门到精通-2
上部分讲到了hosts[*] 匹配所有的微服务,这部分细化一下 在 Istio 的 VirtualService 配置中,hosts 字段用于指定该虚拟服务适用的 目标主机或域名。如果使用具体的域名(如 example.com),则只有请求的主机 域名与 exa…...

第5章:vuex
第5章:vuex 1 求和案例 纯vue版2 vuex工作原理图3 vuex案例3.1 搭建vuex环境错误写法正确写法 3.2 求和案例vuex版细节分析源代码 4 getters配置项4.1 细节4.2 源代码 5 mapState与mapGetters5.1 总结5.2 细节分析5.3 源代码 6 mapActions与mapMutations6.1 总结6.2…...
[Python入门学习记录(小甲鱼)]第5章 列表 元组 字符串
第5章 列表 元组 字符串 5.1 列表 一个类似数组的东西 5.1.1 创建列表 一个中括号[ ] 把数据包起来就是创建了 number [1,2,3,4,5] print(type(number)) #返回 list 类型 for each in number:print(each) #输出 1 2 3 4 5#列表里不要求都是一个数据类型 mix [213,"…...

Docker 学习(四)——Dockerfile 创建镜像
Dockerfile是一个文本格式的配置文件,其内包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。有了Dockerfile,当我们需要定制自己额外的需求时,只需在D…...

Java多线程与高并发专题——为什么 Map 桶中超过 8 个才转为红黑树?
引入 JDK 1.8 的 HashMap 和 ConcurrentHashMap 都有这样一个特点:最开始的 Map 是空的,因为里面没有任何元素,往里放元素时会计算 hash 值,计算之后,第 1 个 value 会首先占用一个桶(也称为槽点ÿ…...
LeetCode hot 100—二叉树的中序遍历
题目 给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。 示例 示例 1: 输入:root [1,null,2,3] 输出:[1,3,2]示例 2: 输入:root [] 输出:[]示例 3: 输入:root […...
代码随想录算法训练营第35天 | 01背包问题二维、01背包问题一维、416. 分割等和子集
一、01背包问题二维 二维数组,一维为物品,二维为背包重量 import java.util.Scanner;public class Main{public static void main(String[] args){Scanner scanner new Scanner(System.in);int n scanner.nextInt();int bag scanner.nextInt();int[…...

与中国联通技术共建:通过obdiag分析OceanBase DDL中的报错场景
中国联通软件研究院(简称联通软研院)在全面评估与广泛调研后,在 2021年底决定采用OceanBase 作为基础,自研分布式数据库产品CUDB(即China Unicom Database,中国联通数据库)。目前,该…...

AI Agent与Agentic AI:原理、应用、挑战与未来展望
文章目录 一、引言二、AI Agent与Agentic AI的兴起2.1 技术契机与生态成熟2.2 Agent的定义与特征2.3 Agent的发展历程 三、AI Agent的核心技术栈解密3.1 感知模块代码示例:使用Python和OpenCV进行图像识别 3.2 认知与决策模块代码示例:使用OpenAI GPT-3进…...
【磁盘】每天掌握一个Linux命令 - iostat
目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat(I/O Statistics)是Linux系统下用于监视系统输入输出设备和CPU使…...

SpringCloudGateway 自定义局部过滤器
场景: 将所有请求转化为同一路径请求(方便穿网配置)在请求头内标识原来路径,然后在将请求分发给不同服务 AllToOneGatewayFilterFactory import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; impor…...
力扣-35.搜索插入位置
题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...

面向无人机海岸带生态系统监测的语义分割基准数据集
描述:海岸带生态系统的监测是维护生态平衡和可持续发展的重要任务。语义分割技术在遥感影像中的应用为海岸带生态系统的精准监测提供了有效手段。然而,目前该领域仍面临一个挑战,即缺乏公开的专门面向海岸带生态系统的语义分割基准数据集。受…...

20个超级好用的 CSS 动画库
分享 20 个最佳 CSS 动画库。 它们中的大多数将生成纯 CSS 代码,而不需要任何外部库。 1.Animate.css 一个开箱即用型的跨浏览器动画库,可供你在项目中使用。 2.Magic Animations CSS3 一组简单的动画,可以包含在你的网页或应用项目中。 3.An…...
纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join
纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...

FFmpeg:Windows系统小白安装及其使用
一、安装 1.访问官网 Download FFmpeg 2.点击版本目录 3.选择版本点击安装 注意这里选择的是【release buids】,注意左上角标题 例如我安装在目录 F:\FFmpeg 4.解压 5.添加环境变量 把你解压后的bin目录(即exe所在文件夹)加入系统变量…...
Linux系统部署KES
1、安装准备 1.版本说明V008R006C009B0014 V008:是version产品的大版本。 R006:是release产品特性版本。 C009:是通用版 B0014:是build开发过程中的构建版本2.硬件要求 #安全版和企业版 内存:1GB 以上 硬盘…...

AI语音助手的Python实现
引言 语音助手(如小爱同学、Siri)通过语音识别、自然语言处理(NLP)和语音合成技术,为用户提供直观、高效的交互体验。随着人工智能的普及,Python开发者可以利用开源库和AI模型,快速构建自定义语音助手。本文由浅入深,详细介绍如何使用Python开发AI语音助手,涵盖基础功…...