当前位置:网站首页>The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
2022-07-30 19:01:00 【snowflakes】
很多小伙伴为了刷题发愁
今天为大家推荐一款刷题神奇哦:刷题面试神器牛客
各大互联网大厂面试真题.从基础到入阶乃至原理刨析类面试题 应有尽有,赶快来装备自己吧!助你面试稳操胜券,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
想和博主一样刷优质面试和算法题嘛,快来刷题面试神器牛客吧,期待与你在牛客相见
边栏推荐
猜你喜欢
CCNA-子网划分(VLSM)
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
C# wpf borderless window add shadow effect
深入浅出边缘云 | 3. 资源配置
SimpleOSS第三方库libcurl与引擎libcurl错误解决方法
Pytorch foundation -- tensorboard use (1)
Codeblocks + Widgets create window code analysis
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
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
kotlin by lazy
随机推荐
requet.getHeader("token") is null
云数据库和本地数据库有什么区别?
LeetCode 练习——关于查找数组元素之和的两道题
crontab中写go run不执行的问题
JsonUtil基于字符串操作josn
6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?
"Ruffian Heng Embedded Bimonthly" Issue 59
MySql中@符号的使用
Chapter 14 Type Information
VBA 连接Access数据库和Excle
国轩高科瑞交所上市:募资近7亿美元 为瑞士今年最大融资项目
【hbuilder】运行不了部分项目 , 打开终端 无法输入指令
积性函数
CCNA-NAT协议(理论与实验练习)
OneFlow source code analysis: Op, Kernel and interpreter
电脑死机的时候,发生了什么?
What kind of framework is friendly to developers?
【每日一道LeetCode】——191. 位1的个数
[TypeScript]编译配置
vxe-table实现复选框鼠标拖动选中