当前位置:网站首页>牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
2022-07-30 18:59:00 【雪芙花】
很多小伙伴为了刷题发愁
今天为大家推荐一款刷题神奇哦:刷题面试神器牛客
各大互联网大厂面试真题。从基础到入阶乃至原理刨析类面试题 应有尽有,赶快来装备自己吧!助你面试稳操胜券,solo全场面试官
一:组队竞赛
题目:题目链接

代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
int sum;
sum =n*3;
int arr[sum];
for(int i=0;i<sum;i++)
{
cin >> arr[i];
}
sort(arr,arr+sum);
long sums =0 ;
int i=sum-2;
while(n--)
{
sums+= arr[i];
i-=2;
}
cout<<sums;
return 0;
}
- 思路:
- 先排序
- 最优解是将最小的一个数和右边两个大数进行组队,这样得到的平均值最大
- 注意:
- 注意排序函数要引用头文件
二:排序子序列
- 题目:题目链接

- 代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n+1];
arr[n]=0;
for(int i=0;i<n;i++)
{
cin >> arr[i];
}
int a=0;
int count =0;
while(a<n)
{
if(arr[a]< arr[a+1])
{
while(a<n && arr[a] <= arr[a+1])
a++;
}
else if(arr[a] == arr[a+1])
{
a++;
continue;
}
else
{
while(a<n && arr[a] >= arr[a+1])
a++;
}
a++;
count++;
}
cout<<count;
return 0;
}
- 思路:
- 先判断是升序还是降序,或者只是相同的
- 假如是升序或者降序,一直++,直到不为升序或者降序为止
- 假如是相同的,则直接++
- 注意:
- 注意 开空间是开 n+1个空间,以防越界
- 注意要有相同情况下的判断,不然会出现这种 情况

假如没有相同情况的判断,会多加一次
三:倒置字符串
- 题目:题目链接

这里就不写常规的写法了,只介绍一种我觉得很巧妙的方法
- 代码:
#include<iostream>
using namespace std;
int main()
{
string s1;
string s2;
cin>> s2;
while(cin>> s1)
{
s2 = s1 + " "+ s2;
}
cout<< s2;
return 0;
}
- 思路:
- 先输入一个单词到s1上
- 再循环输入,每次输入的单词都在上一个单词的前面
- 直到输入,即完成了逆置
- 注意:
- 注意是 s2 = s1 + " "+ s2;
- 启发:
当我们面对一些让我们自己输入的题目时,我们可以先一开始不全部输入进去,一部分一部分的输入,一边输入一边处理
四: 删除公共字符
- 题目:题目链接

- 代码:
#include<iostream>
using namespace std;
int main()
{
string s1;
getline(cin,s1);
string s2;
cin>>s2;
char arr[256]={
0};
for(int i=0;i<s2.size();i++)
{
arr[s2[i]] = 1;
}
auto it = s1.begin();
string ret;
for (it = s1.begin(); it != s1.end();it++)
{
if(arr[*it] == 0)
ret+= *it;
}
cout<<ret;
return 0;
}
- 注意:
可以将要输出的内容保存到一个string里,可以减少很多不必要的运算
五:修理牧场(利用堆 和 哈夫曼树思想的运用)
- 题目:

- 代码:
#include<iostream>
#include <queue>
using namespace std;
int main()
{
int n, tatal = 0;
cin >> n;
priority_queue<int, vector<int>, greater<int>> que;
while (n--)
{
int tem;
cin >> tem;
que.push(tem);
}
while (que.size() > 1)
{
int a = que.top();
que.pop();
int b = que.top();
que.pop();
tatal += a + b;
que.push(a + b);
}
cout << tatal;
return 0;
}
这道题主要考的是对 堆,即优先级队列的运用
- 注意:
注意优先级队列的定义 priority_queue<int,vetcor<int >,great<int>> queue
ps
想和博主一样刷优质面试和算法题嘛,快来刷题面试神器牛客吧,期待与你在牛客相见
边栏推荐
- ctf.show_web5
- "Ruffian Heng Embedded Bimonthly" Issue 59
- 【Prometheus】Prometheus联邦的一次优化记录[续]
- 6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?
- 实体中增加操作方法
- 基于inquirer封装一个控制台文件选择器
- Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!
- Redis for infrastructure
- WeChat Mini Program Cloud Development | Urban Information Management
- Go 系统收集
猜你喜欢

kotlin的by lazy

vxe-table实现复选框鼠标拖动选中

The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova

猎豹移动终于递交年报:年营收7.85亿 腾讯持股16.6%

scrapy基本使用

OneFlow source code analysis: Op, Kernel and interpreter

运营 23 年,昔日“国内第一大电商网站”黄了...

A senior with 13 years of experience in software testing, summed up 5 test employment suggestions....

线性筛求积性函数

node封装一个控制台进度条插件
随机推荐
MYSQL (Basic) - An article takes you into the wonderful world of MYSQL
ROS 环境使用第三方动态链接库(.so)文件
Two-point answer naked question (plus a little pigeonhole principle)
云数据库和本地数据库有什么区别?
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
深入浅出边缘云 | 3. 资源配置
- daily a LeetCode 】 【 191. A number of 1
Graphic LeetCode -- 11. Containers of most water (difficulty: medium)
中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
Chapter 14 Type Information
二分答案裸题(加一点鸽巢原理)
Scrapy框架介绍
LeetCode 练习——关于查找数组元素之和的两道题
Delay queue optimization (2)
MySQL data types
跨域问题的解决方法
OneFlow源码解析:Op、Kernel与解释器
终端分屏工具Terminalx的使用
The sixteenth issue of eight-part article Balabala said (MQ)
Swiper rotates pictures and plays background music