1:流浪地球 0~n-1个发动机,计划启动m次,求最后启动的发动机的个数。 以及发动机的编号。(模拟过程,每次手动启动的机器对应时间向两边扩散)
//输入每个启动的时间和编号
void test_liulang()
{int n, m;cin >> n >> m;if (n > 1000 || m > n){return;}int t, p;vector<pair<int, int>> vec;for (int i = 0; i < m; i++){cin >> t >> p;vec.push_back({ t,p });}//已经存储了发动机的个数 和要启动的发动机个数 已经存储了在时刻启动发动机的信息//打印最后启动的发动机个数 和发动机编号//使用数组标识每个发动机的最终状态vector<int> vec_timer(n, 1001); //为了后面计算方便//每次启动 实际上是x号发动机在y号启动的 依次遍历给取最小值 for (int i = 0; i < vec.size(); i++){//遍历启动的发动机时刻vec_timer[vec[i].second] = vec[i].first; //在这个时刻启动了这个发动机for (int j = 0; j < n; j++){//依次加入关联启动的时间点 其实就是求当前点到点火发动机的距离int innerdir = abs(vec[i].second - j);int outerdir = n - innerdir;vec_timer[j] = min(vec_timer[j], vec_timer[vec[i].second] +min(innerdir, outerdir)); //该发动机启动的时间就是}}//已经对发动及和启动时刻做了保存 //然后就是对最晚启动时间和位置作统计//找到vector的最大值个数和对应的下标int maxtime = -1;for (int i = 0; i < n; i++){if (vec_timer[i] > maxtime){maxtime = vec_timer[i];}cout << "i =" << vec_timer[i]<<" ";}cout << endl;cout << maxtime << endl;for (int i = 0; i < n; i++){if (vec_timer[i] == maxtime){cout << i << " ";}}}
2:栈数据合并/空栈压数 (入栈时合并数据)
临时保存前一个数据和前面所有数据的和。 (这里需要注意 如何保证有效输入 只能getline读取一整行,然后按照字符串进行解析么)
void test_stacknull()
{int num;vector<int> m_vec;while (cin >> num){if (num == -1){break;}m_vec.emplace_back(num); //首先获取用户输入的数据 }//依次遍历入栈vector<int> m_res;m_res.emplace_back(m_vec[0]);int sum = m_vec[0], temp = m_vec[0];for(int i=1; i<m_vec.size(); i++){if (sum == m_vec[i]){m_res.clear();m_res.emplace_back(2 * sum);sum += m_vec[i];temp = 2 * sum;continue;}if (temp == m_vec[i]){m_res[m_res.size() - 1] = 2*temp; //栈顶元素相同}else{m_res.emplace_back(m_vec[i]);}sum += m_vec[i];temp = m_vec[i];}for (int i = m_res.size() - 1; i >= 0; i--){std::cout << m_res[i]<<" ";}
}
3:正则表达式替换(不替换方括号和转义的字符 其他进行替换)
这里需要注意转义字符\的输入处理
void test_split()
{string str = "^(_9494)[_324[fs\_]]sd_\\_2"; //实现替换其中的_ cout << "str = " << str<<endl;string replace_str = "(^|$|[,+])";int nums = 0;string str_res;for (int i = 0; i < str.size(); i++){if (nums > 0) //直接{str_res += str[i];}else //这里肯定等于0了{if (str[i] == '_' && ((i == 0) || str[i - 1] != '\\')){str_res += replace_str;continue;}str_res += str[i];}if (str[i] == '['){++nums;}if (str[i] == ']'){--nums;}}std::cout << str_res;}
4:求大于或者等于n的最小回文素数
首先获取大于n的回文数,再判断是不是质数。 从n开始直接往上判断
5:字符串拼接(递归+回溯)
给出的字符,相同字符不能相邻, 拼成长度n 的字符串
注意思路,开始时字符串是”“,每次再后面加,回溯处理。
void generateDistinctStrings(string source, int length, string cur, unordered_set<string>& result, vector<bool> un_used);//给出字符 让拼接成目标为n长度的字符串,相同字符不能相邻,要求满足条件的个数。
void test_get_string()
{string str;int a;cin >> str >> a;//对字符串进行处理 要求长度为a的字符串生成方法//使用递归+回溯的方案进行实现vector<bool> vec_used(str.length(), false); //保存字符是否被使用unordered_set<string> m_set; //用于存储已经构建好的集合generateDistinctStrings(str, a, "", m_set, vec_used);cout << "size = " << m_set.size();for (string data : m_set){cout << " " << data;}cout << endl;}
//递归+回溯处理 当前字符如果满足条件 放入结果集中 用一个集合标记是否被使用了
void generateDistinctStrings(string source, int length, string cur, unordered_set<string>& result, vector<bool> un_used)
{if (cur.length() == length){result.emplace(cur);return;}//遍历字符串中的字符 for (int i = 0; i < source.length(); i++){//当前字符没有被使用 以及和前面字符不相同if (un_used[i] || (cur.length() > 0 && cur.back() == source[i])){continue;}//标记当前已经使用un_used[i] = true; //递归调用生成下一个字符generateDistinctStrings(source, length, cur + source[i], result, un_used);un_used[i] = false; //取消当前标记 回溯处理}
}
6:数值同化 (BFS 宽度优先搜索)
矩阵 1有同化功能 把上下左右都会同化成1 问最后剩下2的可能
广度优先算法 借助上下左右 和队列 取队列第一个依次上下左右进行处理
//数值同化 统计0和2 的个数 宽度优先算法
//广度优先算法 定义方向 上下左右
const int dx[] = { -1, 1, 0,0 };
const int dy[] = { 0, 0, -1,1 };void test_getnum()
{int n, m;cin >> n>> m;//输入矩阵vector<vector<int>> m_vec(n, vector<int>(m));for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){cin >> m_vec[i][j];}}//m_vec[0][0] = 1;//借助队列来处理相邻和个数queue<pair<int, int>> m_queue;m_queue.push({ 0, 0 }); while (m_queue.empty() == false) //队列不为NULL{auto t = m_queue.front(); //m_queue.pop(); //移出元素 //遍历上下左右for (int i = 0; i < 4; ++i){int nx = t.first + dx[i]; //int ny = t.second + dy[i];if ((nx >= 0 && nx < n && ny >= 0 && ny < m) && m_vec[nx][ny] == 0){m_vec[nx][ny] = 1;m_queue.push({ nx, ny });}}}int count = 0;for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (m_vec[i][j] == 0 || m_vec[i][j] == 2){++count;}}}cout << count << endl;}
7:火星文计算(数以stringstream提取字符串和数字的逻辑)
已知火星人使用的运算符为#、KaTeX parse error: Expected 'EOF', got '#' at position 18: …与地球人的等价公式如下: x#̲y = 4*x+3*y+2 …y = 2*x+y+3
1、其中x、y是无符号整数
2、地球人公式按C语言规则计算
3、火星人公式中,#的优先级高于$,相同的运算符,按从左到右的顺序计算
现有一段火星人的字符串报文,请你来翻译并计算结果。
void test_huoxing()
{// string str = {"123#4$5#67$78"};string str = { "7#6$5#12" };// cin >> str;stack<int> m_num;stack<char> m_ops;stringstream ss(str);int num;char ops;while (ss >> num){m_num.push(num);if (ss >> ops){m_ops.push(ops);}}//先处理#while (!m_ops.empty()){while (!m_ops.empty() && m_ops.top() == '#') //优先级高 先处理{m_ops.pop();int y = m_num.top(); m_num.pop();int x = m_num.top(); m_num.pop();int result = 2* x + 3* y + 4;m_num.push(result);}while (!m_ops.empty() && m_ops.top() == '$') //再处理优先级低的{m_ops.pop();int y = m_num.top(); m_num.pop();int x = m_num.top(); m_num.pop();int result = 3 * x + y + 2;m_num.push(result);}}cout << m_num.top();}
8:最大的整数 (自定义排序规则 重新排序后拼接)
void test_max_num()
{vector<int> data = { 3,30,34,5,9 };//直接借助sort函数 自定义转换比较函数进行处理sort(data.begin(), data.end(), [](int a, int b) {return to_string(a) + to_string(b) > to_string(b) + to_string(a);});if (data[0] == 0){cout << 0 << endl; return ;}string res;for (int i = 0; i < data.size(); i++){res += to_string(data[i]);}cout << res << endl;
}
9:构成正方形的数量 (数学问题 )
输入N个互不相同的二维整数坐标,求这N个坐标可以构成的正方形数量。[内积为零的的两个向量垂直]
输入描述
第一行输入为N,N代表坐标数量,N为正整数。N <= 100
之后的 K 行输入为坐标x y以空格分隔,x,y为整数,-10<=x, y<=10
输出描述
输出可以构成的正方形数量。
示例1
输入
3
1 3
2 4
3 1
1234
输出
0
1
10:靠谱的车(打车遇到4就跳过,求真正)
其实就是求含4的个数 算出来后减去就好
bool if_boo(int a)
{while (a){if (a % 10 == 4){return true;}a = a / 10;}return false;
}//遇到4就跳过
void test_car()
{//输入一个数 是跳过4的结果 求真正的实际值 //实际上就是求这么多数中 有4的个数 然后相减int n = 100;int num = 0;for (int i = 0; i < n; i++){if (if_boo(i)){cout << "num " << i << " " << endl;++num;}}int res = 100 - num;cout <<"data = "<< res <<endl;
}
11:敏感字段加密 (实际上就是用_进行分割,以及”“中间的_不算命令)
没有处理ERROR
void test_command()
{int n = 2;string str = "aaa_password_\"a12_45678\"_timeout__100_\"\"_"; //由于双引号不会再内部出现,所以 只需要判断起始和终止cout << str<<endl;vector<string> m_vec; //命令字从0开始的么//实际上不用切割 只需要找到命令字的起始和终止//1:合并分隔符 2:找到对应分隔符的下标 3:切割替换string dest;int flag = 0;for (int i = 0; i < str.length() - 1; i++){ if (str[i] == '_'&& flag==1){continue;}if (str[i] == '_'){flag = 1;dest += '_';continue;}dest += str[i];if (flag == 1) flag = 0;}if (flag != 1) dest += str[str.length() - 1];cout << str << endl;cout << dest << endl;//找到对应索引的下标int is_flag = 0;int dest_num = 0;int i = 0;for (i = 0; i < dest.length() - 1; i++){if (is_flag == 1 && dest[i] != '\"'){continue;}if (is_flag == 1 && dest[i] == '\"'){is_flag = 0;continue;}if (dest[i] == '\"'){is_flag = 1;}if (dest[i] == '_'){dest_num++;}if (dest_num == n)break;}cout << i << endl;//已经找到了对应的切割下标的位置 进行拆分后拼接string result;result = dest.substr(0, i + 1);result += "******";string last = dest.substr(i + 1);if (last.length() == 0){cout << result << endl;return;}int pos;if (last[0] == '\"'){ last[0] = '_';pos = last.find('\"'); //找到第一个下标}else{pos = last.find('_'); //找到第一个下标}result += last.substr(pos + 1);cout << result << endl;
}
12:TLV编码(多个字符 第一个是编码tag 后两个长度(小段) 在后面是长度)
其实并不复杂 只要细心 注意十六进制的输出相关 printf 02x 以及cout 的std::hex
void test_tlv()
{string dest = "31"; //目标解码字符串string code = "32 01 00 AE 90 02 00 01 02 30 03 00 AB 32 31 31 02 00 32 33 33 01 00 CC";code += ' ';int tag = stoi(dest, nullptr, 16);cout << code << endl;vector<int> m_vec;string src;int num; for (int i = 0; i < code.length(); i++){if (code[i] == ' '){num = stoi(src, nullptr, 16);m_vec.push_back(num);src = "";continue;}src += code[i];}for (int i = 0; i < m_vec.size(); i++){cout << m_vec[i] << " ";}cout << endl;//接下来就是按照协议对里面的字符进行处理 int src_tag = m_vec[0];int k = 0;int len3 = 0;while (true) //这里最好定义结构体来进行处理{if (src_tag == tag){break;}int len1 = m_vec[k+1];int len2 = m_vec[k + 2];//cout << len1 << " " << len2 <<endl;//int len = len1 << 8 | len2;len3 = len2 << 8 | len1; //验证这个结果正确//cout << len << " " << len3 << endl;k += 2 + 1 + len3;cout << "len3= " << len3 <<" k = "<<k << endl;if (k > m_vec.size() - 1){cout << "error" << endl;break;}src_tag = m_vec[k];cout << "tag = " << tag << " src_tag = "<<src_tag << endl;}len3 = m_vec[k + 2] << 8 | m_vec[k + 1];cout << "k=" << k <<" len = "<<len3 << endl;cout << std::hex;for (int i = 0; i < len3; i++){cout << m_vec[k + 3 + i] <<" ";// printf("%d %02x ",k, m_vec[k + 3 + i]);}cout << endl;
}