[一天至少一题直到ICPC开赛008]解题: Vito's Family(12/17)

Vito's Family

cpe 一星题

找中位数

最近很忙先放简单的题目

#include <iostream>#include <vector>#include <map>#include <algorithm>using namespace std;// 2 2 3 4// 2 ==> 0+1+2=3// 3 ==> 0+1+1+1=3// 4 ==> 2+2+1=5// 选最多的获胜int main(){    int t;    cin >> t;    while (t--)    {        int n;        cin >> n;        vector<int> a(n);        for (int i = 0; i < n; i++)        {            cin >> a[i];        }        sort(a.begin(), a.end());                int ans = 0;        // 中位数        for (int i = 0; i < n; i++)        {            ans += abs(a[i] - a[n / 2]);        }        cout << ans << endl;    }    return 0;}

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章