当前位置:网站首页>牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
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

【PHPWord】PHPOffice 套件之PHPWord快速入门

2种手绘风格效果比较,你更喜欢哪一种呢?

深入浅出边缘云 | 3. 资源配置

JS提升:Promise中reject与then之间的关系

《痞子衡嵌入式半月刊》 第 59 期

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

微信小程序云开发 | 城市信息管理

CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配

"Ruffian Heng Embedded Bimonthly" Issue 59
随机推荐
高并发秒杀项目总结
单例模式 (Singleton)
深入浅出边缘云 | 3. 资源配置
【剑指 Offer】剑指 Offer 22. 链表中倒数第k个节点
《痞子衡嵌入式半月刊》 第 59 期
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
MySQL data types
攻防世界web-Cat
A senior with 13 years of experience in software testing, summed up 5 test employment suggestions....
Two-point answer naked question (plus a little pigeonhole principle)
CCNA-ACL(访问控制列表)标准ACL 扩展ACL 命名ACL
MYSQL (Basic) - An article takes you into the wonderful world of MYSQL
MongoDB打破了原则引入SQL?
What is the value of biomedical papers? How to translate the papers into Chinese and English?
【Qt Designer工具的使用】
Anaconda Navigator stuck on loading applications
沉浸式体验科大讯飞2022消博会“官方指定产品”
常见链表题及其 Go 实现
Graphic LeetCode -- 11. Containers of most water (difficulty: medium)
requet.getHeader("token") is null