UVA 10041 Vito's Family 求中位数 模拟
2013-09-06 21:32
357 查看
很水,就是找出一个数,让它与数组各数的距离和最小。
很明显是中位数,模拟即可。
代码:
很明显是中位数,模拟即可。
代码:
/* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.net/hcbbt * File: uva10041.cpp * Lauguage: C/C++ * Create Date: 2013-09-04 23:51:13 * Descripton: UVA 10041 Vito's Family, mid */ #include <cstdio> #include <algorithm> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) const int MAXN = 510; int a[MAXN]; int main() { int n, t, m; scanf("%d", &t); while(t--) { scanf("%d", &n); rep(i, n) scanf("%d", &a[i]); sort(a, a + n); if (n % 2) m = a[n / 2]; else m = a[n / 2 - 1]; int sum = 0; rep(i, n) sum += abs(a[i] - m); printf("%d\n", sum); } return 0; }
相关文章推荐
- uva10041 - Vito's Family(求中位数)
- UVa 10041 Vito's Family (中位数&快速选择)
- UVA - 10041 Vito's Family 求中位数
- uva - 10041 - Vito's Family(数学)
- UVA - 10041 Vito's Family(贪心)
- UVa Problem Solution: 10041 - Vito's Family
- uva 10041 Vito's Family_贪心
- UVA - 10041 Vito's Family (中位数)
- UVA 10041 Vito's Family (中位数)
- 第K小数 uva 10041 - Vito's Family poj 2388 Who's in the Middle
- 排序 UVA 10041 Vito's Family
- UVa 10041 - Vito's Family
- (挑战编程_4_1)Vito's Family
- UVA 10041 Vito's Family (中位数)
- UVa 10041 Vito's Family (median selection)
- UVa 10041 - Vito's Family
- UVA10041 - Vito's family
- UVA10205 - Stack 'em Up(模拟)
- 【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁
- [模拟水题&&中位数]uva10041 Vito's Family