牛客算法周周练17C - 成绩分析(签到题)

链接:https://ac.nowcoder.com/acm/contest/6607/C
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

给出一个班级所有同学的成绩,请求出采用平均数和中位数作为班级成绩的差异。

输入描述:

第1行输入整数n(1≤n≤100),代表班级人数。

第2行输入班级n个同学的成绩,每个同学的成绩为[0,100]范围内的其中一个整数。成绩按照从小到大的顺序排列。

输出描述:

输出一个非负整数,为采用平均数和中位数作为班级成绩的差异(差异指的是两者差值的绝对值)。结果保证为非负整数。
示例1
输入
5
80 81 90 95 99
输出
1
说明
平均值为89分,中位数为90分,两者相差1分。

解题思路:

求一下平均分中位数取差的绝对值就行了。

Code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <cstring>
using namespace std;
const int N = 150;
int a[N];
int main()
{
	int n, sum = 0;
	cin >> n;
	for (int i = 1; i <= n; i ++)
	{
		cin >> a[i];
		sum += a[i];
	}
	if (n & 1)
	{
		int ans = abs(sum / n - a[n / 2 + 1]);
		cout << ans << endl;
	}
	else
	{
		int ans = abs(sum / n - (a[n / 2] + a[n / 2 + 1]) / 2);
		cout << ans << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值