• 牛客算法周周练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;
    }
    
  • 相关阅读:
    [NOI2005]维修数列
    [USACO07OPEN]吃饭Dining
    [TJOI2010]打扫房间
    [SCOI2005]最大子矩阵
    [HNOI2007]最小矩形覆盖
    [HAOI2006]受欢迎的牛
    BZOJ2087[Poi2010] Sheep
    [USACO08DEC]在农场万圣节Trick or Treat on the Farm
    [POI2013]BAJ-Bytecomputer
    HGOI20190126 模拟赛
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294175.html
Copyright © 2020-2023  润新知