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

机试准备第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天

首先学习二分搜索法。使用二分查找需要先排序。第一题是查找&#xff0c;现学现卖。 //二分查找 #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 介绍功能概览多种图表类型- **基础类型**&#xff1a;折线图、柱状图、饼图、散点图。- **高级类型**&#xff1a;雷达图、热力图、桑基图、K线图。- **地理可视化**&#xff1a;支持地图&#xff08;如中国、世界&#xff09;和地理坐标系。- **3D支持…...

最新版本TOMCAT+IntelliJ IDEA+MAVEN项目创建(JAVAWEB)

前期所需&#xff1a; 1.apache-tomcat-10.1.18-windows-x64&#xff08;tomcat 10.1.8版本或者差不多新的版本都可以&#xff09; 2.IntelliJ idea 24年版本 或更高版本 3.已经配置好MAVEN了&#xff08;一定先配置MAVEN再搞TOMCAT会事半功倍很多&#xff09; 如果有没配置…...

Linux - 进程通信

一、管道 管道是一种进程间通信&#xff08;IPC&#xff09;机制&#xff0c;用于在进程之间传递数据。它的本质是操作系统内核维护的一个内存缓冲区&#xff0c;配合文件描述符进行数据的读写。尽管管道的核心是内存缓冲区&#xff0c;但操作系统通过对管道的实现&#xff0c…...

使用 Arduino 的 WiFi 控制机器人

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

网络安全等级保护2.0 vs GDPR vs NIST 2.0:全方位对比解析

在网络安全日益重要的今天&#xff0c;各国纷纷出台相关政策法规&#xff0c;以加强信息安全保护。本文将对比我国网络安全等级保护2.0、欧盟的GDPR以及美国的NIST 2.0&#xff0c;分析它们各自的特点及差异。 网络安全等级保护2.0 网络安全等级保护2.0是我国信息安全领域的一…...

verb words

纠正correct remedy 修正modify 协商 confer 磋商/谈判 negotiate 通知notice notify *宣布announce 声明declare 宣告 declare *颁布 promulgate /introduce 协调coordinate 评估evaluate assess 撤离evacuate *规定stipulate 参与participate&#xff0c; 涉及refer…...

unity console日志双击响应事件扩展

1 对于项目中一些比较长的日志&#xff0c;比如前后端交互协议具体数据等&#xff0c;这些日志内容可能会比较长&#xff0c;在unity控制面板上查看不是十分方便&#xff0c;我们可以对双击事件进行扩展&#xff0c;将日志保存到一个文本中&#xff0c;然后用系统默认的文本查看…...

维度建模维度表技术基础解析(以电商场景为例)

维度建模维度表技术基础解析(以电商场景为例) 维度表是维度建模的核心组成部分,其设计直接影响数据仓库的查询效率、分析灵活性和业务价值。本文将从维度表的定义、结构、设计方法及典型技术要点展开,结合电商场景案例,深入解析其技术基础。 1. 维度表的定义与作用 定义…...

Leetcode 264-丑数/LCR 168/剑指 Offer 49

题目描述 我们把只包含质因子 2、3 和 5 的数称作丑数&#xff08;Ugly Number&#xff09;。求按从小到大的顺序的第 n 个丑数。 示例: 说明: 1 是丑数。 n 不超过1690。 题解 动态规划法 根据题意&#xff0c;每个丑数都可以由其他较小的丑数通过乘以 2 或 3 或 5 得到…...

阿里云MaxCompute面试题汇总及参考答案

目录 简述 MaxCompute 的核心功能及适用场景,与传统数据仓库的区别 解释 MaxCompute 分层架构设计原则,与传统数仓分层有何异同 MaxCompute 的存储架构如何实现高可用与扩展性 解析伏羲(Fuxi)分布式调度系统工作原理 盘古(Pangu)分布式存储系统数据分片策略 计算与存…...

笔记:Directory.Build.targets和Directory.Build.props的区别

一、目的&#xff1a;分享Directory.Build.targets和Directory.Build.props的区别 Directory.Build.targets 和 Directory.Build.props 是 MSBuild 的两个功能&#xff0c;用于在特定目录及其子目录中的所有项目中应用共享的构建设置。它们的主要区别在于应用的时机和用途。 二…...

istio入门到精通-2

上部分讲到了hosts[*] 匹配所有的微服务&#xff0c;这部分细化一下 在 Istio 的 VirtualService 配置中&#xff0c;hosts 字段用于指定该虚拟服务适用的 目标主机或域名。如果使用具体的域名&#xff08;如 example.com&#xff09;&#xff0c;则只有请求的主机 域名与 exa…...

第5章:vuex

第5章&#xff1a;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是一个文本格式的配置文件&#xff0c;其内包含了一条条的指令(Instruction)&#xff0c;每一条指令构建一层&#xff0c;因此每一条指令的内容&#xff0c;就是描述该层应当如何构建。有了Dockerfile&#xff0c;当我们需要定制自己额外的需求时&#xff0c;只需在D…...

Java多线程与高并发专题——为什么 Map 桶中超过 8 个才转为红黑树?

引入 JDK 1.8 的 HashMap 和 ConcurrentHashMap 都有这样一个特点&#xff1a;最开始的 Map 是空的&#xff0c;因为里面没有任何元素&#xff0c;往里放元素时会计算 hash 值&#xff0c;计算之后&#xff0c;第 1 个 value 会首先占用一个桶&#xff08;也称为槽点&#xff…...

LeetCode hot 100—二叉树的中序遍历

题目 给定一个二叉树的根节点 root &#xff0c;返回 它的 中序 遍历 。 示例 示例 1&#xff1a; 输入&#xff1a;root [1,null,2,3] 输出&#xff1a;[1,3,2]示例 2&#xff1a; 输入&#xff1a;root [] 输出&#xff1a;[]示例 3&#xff1a; 输入&#xff1a;root […...

代码随想录算法训练营第35天 | 01背包问题二维、01背包问题一维、416. 分割等和子集

一、01背包问题二维 二维数组&#xff0c;一维为物品&#xff0c;二维为背包重量 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中的报错场景

中国联通软件研究院&#xff08;简称联通软研院&#xff09;在全面评估与广泛调研后&#xff0c;在 2021年底决定采用OceanBase 作为基础&#xff0c;自研分布式数据库产品CUDB&#xff08;即China Unicom Database&#xff0c;中国联通数据库&#xff09;。目前&#xff0c;该…...

IDEA 接入 Deepseek

在本篇文章中&#xff0c;我们将详细介绍如何在 JetBrains IDEA 中使用 Continue 插件接入 DeepSeek&#xff0c;让你的 AI 编程助手更智能&#xff0c;提高开发效率。 一、前置准备 在开始之前&#xff0c;请确保你已经具备以下条件&#xff1a; 安装了 JetBrains IDEA&…...

斗地主小游戏

<!DOCTYPE html> <html><head><meta charset="utf-8"><title>斗地主</title><style>.game-container {width: 1000px;height: 700px;margin: 0 auto;position: relative;background: #35654d;border-radius: 10px;padding…...

如何改变怂怂懦弱的气质(2)

你是否曾经因为害怕失败而逃避选择&#xff1f;是否因为不敢拒绝别人而让自己陷入困境&#xff1f;是否因为过于友善而被人轻视&#xff1f;如果你也曾为这些问题困扰&#xff0c;那么今天的博客就是为你准备的。我们将从行动、拒绝、自我认知、实力提升等多个角度&#xff0c;…...

C# OnnxRuntime部署DAMO-YOLO人头检测

目录 说明 效果 模型信息 项目 代码 下载 参考 说明 效果 模型信息 Model Properties ------------------------- --------------------------------------------------------------- Inputs ------------------------- name&#xff1a;input tensor&#xff1a;Floa…...

基于GeoTools的GIS专题图自适应边界及高宽等比例生成实践

目录 前言 一、原来的生成方案问题 1、无法自动读取数据的Bounds 2、专题图高宽比例不协调 二、专题图生成优化 1、直接读取矢量数据的Bounds 2、专题图成果抗锯齿 3、专题成果高宽比例自动调节 三、总结 前言 在当今数字化浪潮中&#xff0c;地理信息系统&#xff08;…...

各种DCC软件使用Datasmith导入UE教程

3Dmax: 先安装插件 https://www.unrealengine.com/zh-CN/datasmith/plugins 左上角导出即可 虚幻中勾选3个插件,重启引擎 左上角选择文件导入即可 Blender导入Datasmith进UE 需要两个插件, 文章最下方链接进去下载安装即可 一样的,直接导出,然后UE导入即可 C4D 直接保存成…...

尚硅谷爬虫note15

一、当当网 1. 保存数据 数据交给pipelines保存 items中的类名&#xff1a; DemoNddwItem class DemoNddwItem(scrapy.Item): 变量名 类名&#xff08;&#xff09; book DemoNddwItem(src src, name name, price price)导入&#xff1a; from 项目名.items import 类…...

云原生系列之本地k8s环境搭建

前置条件 Windows 11 家庭中文版&#xff0c;版本号 23H2 云原生环境搭建 操作系统启用wsl(windows subsystem for linux) 开启wsl功能&#xff0c;如下图 安装并开启github加速器 FastGithub 2.1 下载地址&#xff1a;点击下载 2.2 解压安装文件fastgithub_win-x64.zip 2…...

关于tomcat使用中浏览器打开index.jsp后中文显示不正常是乱码,但英文正常的问题

如果是jsp文件就在首行加 “<% page language"java" contentType"text/html; charsetUTF-8" pageEncoding"UTF-8" %>” 如果是html文件 在head标签加入&#xff1a; <meta charset"UTF-8"> 以jsp为例子&#xff0c;我们…...

mysql foreign_key_checks

‌foreign_key_checks‌是一个用于设置是否在DML/DDL操作中检查外键约束的系统变量。该变量默认启用&#xff0c;通常在正常操作期间启用以强制执行参照完整性。 功能描述 foreign_key_checks用于控制是否在DML&#xff08;数据操纵语言&#xff09;和DDL&#xff08;数据定义…...