机试准备第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智能安防平台
前言: 通过AI视觉技术,为船厂提供全面的安全监控解决方案,涵盖交通违规检测、起重机轨道安全、非法入侵检测、盗窃防范、安全规范执行监控等多个方面,能够实现对应负责人反馈机制,并最终实现数据的统计报表。提升船厂…...
Admin.Net中的消息通信SignalR解释
定义集线器接口 IOnlineUserHub public interface IOnlineUserHub {/// 在线用户列表Task OnlineUserList(OnlineUserList context);/// 强制下线Task ForceOffline(object context);/// 发布站内消息Task PublicNotice(SysNotice context);/// 接收消息Task ReceiveMessage(…...

相机Camera日志实例分析之二:相机Camx【专业模式开启直方图拍照】单帧流程日志详解
【关注我,后续持续新增专题博文,谢谢!!!】 上一篇我们讲了: 这一篇我们开始讲: 目录 一、场景操作步骤 二、日志基础关键字分级如下 三、场景日志如下: 一、场景操作步骤 操作步…...
多场景 OkHttpClient 管理器 - Android 网络通信解决方案
下面是一个完整的 Android 实现,展示如何创建和管理多个 OkHttpClient 实例,分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...
linux 下常用变更-8
1、删除普通用户 查询用户初始UID和GIDls -l /home/ ###家目录中查看UID cat /etc/group ###此文件查看GID删除用户1.编辑文件 /etc/passwd 找到对应的行,YW343:x:0:0::/home/YW343:/bin/bash 2.将标红的位置修改为用户对应初始UID和GID: YW3…...
安卓基础(aar)
重新设置java21的环境,临时设置 $env:JAVA_HOME "D:\Android Studio\jbr" 查看当前环境变量 JAVA_HOME 的值 echo $env:JAVA_HOME 构建ARR文件 ./gradlew :private-lib:assembleRelease 目录是这样的: MyApp/ ├── app/ …...

Linux 内存管理实战精讲:核心原理与面试常考点全解析
Linux 内存管理实战精讲:核心原理与面试常考点全解析 Linux 内核内存管理是系统设计中最复杂但也最核心的模块之一。它不仅支撑着虚拟内存机制、物理内存分配、进程隔离与资源复用,还直接决定系统运行的性能与稳定性。无论你是嵌入式开发者、内核调试工…...
C++.OpenGL (20/64)混合(Blending)
混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...

Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement
Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement 1. LAB环境2. L2公告策略2.1 部署Death Star2.2 访问服务2.3 部署L2公告策略2.4 服务宣告 3. 可视化 ARP 流量3.1 部署新服务3.2 准备可视化3.3 再次请求 4. 自动IPAM4.1 IPAM Pool4.2 …...
Python网页自动化Selenium中文文档
1. 安装 1.1. 安装 Selenium Python bindings 提供了一个简单的API,让你使用Selenium WebDriver来编写功能/校验测试。 通过Selenium Python的API,你可以非常直观的使用Selenium WebDriver的所有功能。 Selenium Python bindings 使用非常简洁方便的A…...