华为OD机试知识点1周速成版——如果考试时间只有1周,不妨看看这篇文章_华为od机考时间不够了-优快云博客
命名规范
字符串:inputString
目标字符:targetChar
计数:count
总和:sum
非常关键
- 分清楚leetcode模式的return和acm模式的cout输出
- 注意观察变量范围,int可能不够
输入
输入字符串
getline(cin, inputString);
//可以带空格
//ABCabc
//hello world
输入多个字符串
#include <iostream>
#include<sstream>
using namespace std;
int main()
{
string str;
int n;
cin >> n;
cin.ignore(); // 清除输入缓冲区中的换行符
string word;
string output;
while(n--){
getline(cin, str);
stringstream ss(str);
output = ""; // 每次循环前清空output
while(ss >> word){
output += toupper(word[0]);
}
std::cout << output << std::endl;
}
}
注意:
- cin>>n 的输入会有换行符,可能会记录到字符串中,需要清楚
cin.ignore();
- 字符串取出空格等
while(ss >> word){
output += toupper(word[0]);
}
去除换行符
cin.ignore();
getchar();
用符号分割数据串
//
// Created by Dell on 2024/4/3.
//
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
string str;
getline(cin, str);
istringstream iss(str);
string num;
vector<int> nums;
while(getline(iss, num, ',')){
nums.push_back(stoi(num));
}
for(int n : nums){
cout << n << " ";
}
}
注意:
#include <vector>
...
istringstream iss(str);
string num;
getline(iss, num, ',');
nums.push_back(stoi(num)); //stoi:字符串转数据类型
连续输入多个数字
先输入一个整数,再连续多个输入
while(inputNum--){
cin >> num;
auto it = find(nums.begin(), nums.end(), num);
if(it == nums.end()){
nums.push_back(num);
}
}
在终端的一行中输入非固定数目的整型数字,并存到数组中,中间以空格(或者其他单字符,./)分隔。
vector<int> nums;
int num;
while(cin>>num){
nums.push_back(num);
if(getchar() == '\n')
break;
}
输入数组
int n ,m;
cin >> n>>m;
vector<int> nums;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
nums.push_back(a);
}
注意:注意ss >> num 和 nums 分开
哈希表初始化
unordered_map<char, string> charMap = {
{'2', "abc"},{'3', "def"},{'4', "ghi"},{'5', "jkl"},{'6', "mno"},{'7', "pqrs"},{'8', "tuv"},{'9', "wxyz"}};
哈希表键值对存储
vector<pair<char, int>> charCountVec(mp.begin(), mp.end());
键值对排序
bool compare(std::pair<char, int>& a, std::pair<char, int>& b) {
if (a.second == b.second) {
return a.first < b.first;
}
return a.second > b.second;
}
std::vector<std::pair<char, int>> charCountVec(charCount.begin(), charCount.end());
std::sort(charCountVec.begin(), charCountVec.end(), compare);
哈希表键值对遍历
for(auto& pair: charCountVec){
output += pair.first;
}
// 遍历输出map中的所有键值对
for (const auto& pair : myMap) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}
// 遍历输出map中的所有键值对
for (auto it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
}
pair.first:键
pair.second:值
哈希表查找
// 查找map中的元素
std::string key = "banana";
auto it = myMap.find(key);
if (it != myMap.end()) {
std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
} else {
std::cout << "Key not found" << std::endl;
}
vector查找
#include<algorithm>
find(candidates.begin(), candidates.end(), vote) != candidates.end()
map.count
用法:map_name.count(key)
功能:如果在映射容器中存在带有键K的元素,则该函数返回1。如果容器中不存在键为K的元素,则返回0。
count
函数返回指定键在std::map
中出现的次数(0或1)
erase
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
swap
交换函数,常用于交换数组中两个数。
pop_back与erase
**erase()**可以删除由一个iterator指出的元素,也可以删除一个指定范围的元素。
iterator erase (const_iterator position);//删除指定位置元素
iterator erase (const_iterator first, const_iterator last);//删除起始到结束一段元素
**pop_back()**删除容器内的最后一个元素,容器的size减1.
常用函数
append
inputString.append(padding, '0');
参数1:长度
参数2:字符
substr
inputString.substr(start, 8)
参数1:起始坐标
参数2:结束坐标
- 形式 : s.substr(pos, len)
- 返回值:string,包含s中从pos开始的len个字符的拷贝(pos的默认值是0,len的默认值是s.size() - pos,即不加参数会默认拷贝整个s)
sort
sort(v.begin(),v.end());//升序,默认
sort(v.begin(),v.end(),greater<int>());//降序排列
sort(charCountVec.begin(), charCountVec.end(), cmp); //排序函数,构造lamdal表达式
bool cmp(pair<char, int>& a, pair<char, int>& b){
if(a.second == b.second){ //值相等,按照正常键(ascii)排序
return a.first < b.first;
}
return a.second > b.second; //否则,按照值(次数大小)排序
}
to_string(n)
string numStr = to_string(n);
数据类型转字符串
accumulate
accumulate(dp.begin(), dp.end(), 0);//求dp序列所有元素之和,累加初值为0
字符串转数字 stoi
// 将子字符串转换为整数,流控
int number;
std::istringstream iss(numberStr);
iss >> number;
//stoi 转整型
int stoi( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
//base:进制,10:十进制,8:八进制,16:十六进制,0:则自动检测数值进制,str 是 0 开头为八进制,str 是 0x 或 0X 开头是十六进制,默认为十进制
int num = std::stoi(str);
int num = std::stol(str);
int num = std::stoll(str);
字符转数字/数字转字符
int charToInt(char c) {
return c - '0';
}
char intToChar(int num) {
return '0' + num;
}
字符常用函数
int isxdigit(int ch): 判断读入的字符是否是十六进制数字,是返回128,不是返回0
int isalpha(int ch):判断ch是否为字母
int islower(int ch):判断ch是否为小写字母
int isupper(int ch): 判断ch是否为大写字母
int isalnum(int ch): 判断ch是否为字母或数字
int isspace(int ch): 判断ch是否为空白字符(包括空格,换页符\f,换行符\n,回车符\r,水平制表符\t,垂直制表符\v)
int iscntrl(int ch): 判断ch是否为控制字符(控制字符不可打印)
int isgraph(int ch): 判断ch是否为可打印字符(不包括空格)
int isprint(int ch): 判断ch是否为可打印字符(包括空格)
int ispunct(int ch): 判断ch是否为标点符号(包括除了空格、字母和数字之外的所有可打印字符)
int tolower(int ch):将参数ch中的大写字母转换为小写字母,如果不是字母保持原有值不变
int toupper(int ch):将参数ch中的小写字母转换为大写字母,如果不是字母保持原有值不变
插入子串:s1.insert(子串插入位置,要插入的子串)
替换子串:s1.replace(要替换子串的起始位置,要替换子串的长度,新子串)
fixed 和 setprecision
cout << std::fixed << std::setprecision(1) << average << std::endl;
std::fixed
和 std::setprecision
是用于控制输出格式的流控制器(stream manipulators)。
std::fixed
:这个流控制器用于设置浮点数的输出格式为固定小数点表示法。当使用std::fixed
后,浮点数将以固定小数点的形式输出,即始终显示小数点后的位数,即使是0。例如,3.0
将输出为3.000000
。std::setprecision
:这个流控制器用于设置浮点数的输出精度,即小数点后的位数。通过std::setprecision(n)
可以设置浮点数的输出精度为n
位小数。例如,std::setprecision(2)
将设置浮点数的输出精度为2位小数。
类型转换操作符
new_type static_cast <new_type> (expression)
long long square = static_cast<long long>(n) * n;
e.g.
double avrg = major > 0 ? static_cast<double>(sum) / major : 0.0;
priority_queue
#include<queue>
priority_queue<int> q;
//升序队列 小顶堆 great 小到大
priority_queue <int,vector<int>,greater<int> > pri_que;
//降序队列 大顶堆 less 大到小 默认
priority_queue <int,vector<int>,less<int> > pri_que;
- empty
- top
- size
- pop
- emplace
- push
- swap
lower_bound & upper_bound
- lower_bound用于查找第一个大于或等于特定值的目标元素。如果找不到这样的元素,lower_bound会返回一个指向容器末尾的迭代器。
- upper_bound则用于查找第一个严格大于特定值的目标元素。同样,如果找不到这样的元素,upper_bound也会返回一个指向容器末尾的迭代器。
emplace_back和push back的区别
emplace_back的优势在于它可以原地构造对象,避免了额外的拷贝或移动操作。然而,如果已经有一个对象实例并且只需要将其添加到向量中,使用push_back就足够了,因为这种情况下,性能差异可能可以忽略不计。
注意
对于C++来说,0xAA
是一个十六进制整数的表示方式,其中的0x
前缀表示这是一个十六进制数。当您输入0xAA
时,实际上是一个十六进制数的表示,程序会将其识别为一个十六进制数值,而不是字符串。因此,在程序中处理这个输入时,无需考虑0x
前缀。
十六进制到十进制的转换式
decimal = decimal * 16 + hexToInteger[c];
质数
while (num % i == 0) {
factors.push_back(i);
num /= i;
}
最大公约数
long long gcd = std::gcd(numerator, denominator);
最小公倍数
lcm(10,20) = 20