当前位置:网站首页>力扣每日一题-第29天-1491.去掉最低工资和最高工资后的平均工资
力扣每日一题-第29天-1491.去掉最低工资和最高工资后的平均工资
2022-06-28 02:43:00 【重邮研究森】
2022.6.27今天你刷题了吗?
题目:
给你一个整数数组 salary ,数组里每个数都是 唯一 的,其中 salary[i] 是第 i 个员工的工资。
请你返回去掉最低工资和最高工资以后,剩下员工工资的平均值。
分析:
给定一个数组,去掉最大和最小,剩下的数进行求平均并且返回这个平均值。
思路:先排序然后去掉下标0和下标【size()】的数,然后把剩下数通过for加起来
解析:
1.暴力求解
class Solution {
public:
double average(vector<int>& salary) {
sort(salary.begin(), salary.end());
double sum=0.0;
for (int i = 1; i < salary.size()-1; i++)
{
sum += salary[i];
}
return sum/(salary.size()-2);
}
};2.内置函数
通过max_element
min_element
accumulate
三个函数分别取得最大,最小,和累加得到结果
class Solution {
public:
double average(vector<int>& salary) {
double maxValue = *max_element(salary.begin(), salary.end());
double minValue = *min_element(salary.begin(), salary.end());
double sum = accumulate(salary.begin(), salary.end(), - maxValue - minValue);
return sum / int(salary.size() - 2);
}
};边栏推荐
- Is your IOT security strong enough?
- TypeScript 联合类型
- Idea auto generate code
- 2022 operation of simulated examination platform of special operation certificate examination question bank for safety management personnel of hazardous chemical business units
- Dataloader参数collate_fn的使用
- 如何给Eclips自动添加作者,时间等…
- composition api在项目中的使用总结
- Brief history and future trend of codeless software
- 《Go题库·12》slice和array区别?
- Build your own website (17)
猜你喜欢
随机推荐
大咖说·计算讲谈社|什么是东数西算要解决的核心问题?
Documentation issues
数据库系列之MySQL中的分页查询优化
SSH框架的搭建(下)
Notepad++--常用的插件
Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red
爱普生L3153打印机如何清洗喷头
ETCD数据库源码分析——集群间网络层服务端RaftHandler
2022安全员-C证考试题库模拟考试平台操作
同样是MB,差距怎么这么大呢?
TypeScript 联合类型
Question bank and answers of special operation certificate for R1 quick opening pressure vessel operation in 2022
劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)
启牛商学院赠送证券账户是真的吗?开户到底安不安全呢
__ getitem__ And__ setitem__
matlab习题 —— 数据的基本处理
crond BAD FILE MODE /etc/cron.d
基于 WPF 的酷炫 GUI 窗口的简易实现
2022 safety officer-c certificate examination question bank simulated examination platform operation
启牛开的证券账户是安全的吗?如何开账户呢









