(挑战编程_4_1)Vito's Family
2013-05-30 10:34
204 查看
http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110401&format=html
#include <iostream> #include <cstdlib> #include <vector> #include <set> #include <string> #include <fstream> #include <algorithm> using namespace std; //#define _MY_DEBUG_ // 距离Vector int numbers[510]; /* MainStructure: 主结构 */ void MainStructure(); /* Process:处理每一组数据 */ void Process(); int GetDistance(int medium, int numCount); /* 测试输入 */ void Test(); int main() { MainStructure(); return 0; } void Test() { int num; while (cin >> num) { cout << num << endl; } } void MainStructure() { #ifdef _MY_DEBUG_ string inputStr = "C:/Users/cao/Desktop/算法/要看的书/算法/2挑战编程/input/1.inp"; //string inputStr = "D:/2挑战编程/input/110205.inp"; string outputStr = "C:/Users/cao/Desktop/算法/要看的书/算法/2挑战编程/input/0000001.outp"; ifstream fin; fin.open(inputStr); cin.rdbuf(fin.rdbuf()); ofstream fout; fout.open(outputStr); //cout.rdbuf(fout.rdbuf()); #endif // Test(); Process(); } void Process() { // 测试数据的组数 int count; cin >> count; while (count--) { int numCount = 0; cin >> numCount; for (int i = 0; i < numCount; ++i) { cin >> numbers[i]; } sort(numbers, numbers + numCount); int medium = 0; if (numCount & 1) { medium = numbers[numCount / 2]; } else { medium = (numbers[numCount / 2] + numbers[numCount / 2 - 1]) / 2; } cout << GetDistance(medium, numCount) << endl; } } int GetDistance(int medium, int numCount) { int total = 0; for (int i = 0; i < numCount; ++i) { total += abs(numbers[i] - medium); } return total; }
相关文章推荐
- Vito's Family
- UVA - 10041 Vito's Family(贪心)
- UVA 10041 Vito's Family 求中位数 模拟
- UVA - 10041 Vito's Family 求中位数
- (挑战编程_2_5)Stack 'em Up
- uva10041 - Vito's Family(求中位数)
- 爱学习,爱编程,爱咖啡可乐 爱挑战,爱钻研,爱打游戏 爱晚起,也爱工作到深夜 我擅长技术,崇尚简单和懒惰 我神秘而孤僻,沉默而爱憎分明 Don't Panic! I'm a programmer
- (挑战编程_3_2)Where's Waldorf?
- uva 10041 Vito's Family_贪心
- UVa 10041 Vito's Family (中位数&快速选择)
- uva - 10041 - Vito's Family(数学)
- UVa Problem Solution: 10041 - Vito's Family
- Duff's Device 神奇的编程 将字符串拷贝性能提升8倍
- JAVA研修之java编程中'为了性能'一些尽量做到的地方
- 安卓JNI编程-- :error: base operand of '->' has non-pointer type '_JNIEnv' 解决办法
- Turbo C 编程中“Unable to open include file 'stdio.h'”问题的解决
- 32位嵌入式处理器编程:warning: integer constant is too large for 'long' type
- 第K小数 uva 10041 - Vito's Family poj 2388 Who's in the Middle
- Groovy高效编程——‘匕首方法'
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法