当前位置:网站首页>牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
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
想和博主一样刷优质面试和算法题嘛,快来刷题面试神器牛客吧,期待与你在牛客相见
边栏推荐
猜你喜欢
Recommended Books | Recommend 3 database books with rave reviews
开心的聚餐
Node encapsulates a console progress bar plugin
Recommendation | People who are kind to you, don't repay them by inviting them to eat
MySQL data types
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
natural language processing nltk
VBA 连接Access数据库和Excle
猎豹移动终于递交年报:年营收7.85亿 腾讯持股16.6%
VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
随机推荐
Read the "Language Model" in one article
【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
LeetCode Exercise - Two Questions About Finding Sum of Array Elements
【PHPWord】Quick Start of PHPWord in PHPOffice Suite
【网站放大镜效果】两种方式实现
【Qt Designer工具的使用】
Recommendation | People who are kind to you, don't repay them by inviting them to eat
自然语言处理nltk
CCNA-NAT协议(理论与实验练习)
MYSQL (Basic) - An article takes you into the wonderful world of MYSQL
The use of @ symbol in MySql
深入浅出边缘云 | 3. 资源配置
CCNA-ACL(访问控制列表)标准ACL 扩展ACL 命名ACL
积性函数
432.4 FPS 快STDC 2.84倍 | LPS-Net 结合内存、FLOPs、CUDA实现超快语义分割模型
一文读懂“语言模型”
Two-point answer naked question (plus a little pigeonhole principle)
【总结】1396- 60+个 VSCode 插件,打造好用的编辑器
沉浸式体验科大讯飞2022消博会“官方指定产品”
你好,我的新名字叫“铜锁/Tongsuo”