博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[结题报告]10041 - Vito's Family Time limit: 3.000 seconds
阅读量:7238 次
发布时间:2019-06-29

本文共 1374 字,大约阅读时间需要 4 分钟。


  Problem C: Vito's family 

 

Background 

The world-known gangster Vito Deadstone is moving to New York. He has a very big family there, all of them living in Lamafia Avenue. Since he will visit all his relatives very often, he is trying to find a house close to them.

 

Problem 

Vito wants to minimize the total distance to all of them and has blackmailed you to write a program that solves his problem.

 

Input 

The input consists of several test cases. The first line contains the number of test cases.

For each test case you will be given the integer number of relatives r ( 0 < r < 500) and the street numbers (also integers) $s_1, s_2, \ldots, s_i, \ldots, s_r$ where they live ( 0 < si < 30000 ). Note that several relatives could live in the same street number.

 

Output 

For each test case your program must write the minimal sum of distances from the optimal Vito's house to each one of his relatives. The distance between two street numbers si and sj is dij= |si-sj|.

 

Sample Input 

22 2 4 3 2 4 6

Sample Output 

24 参考代码: 这道题求这个人家搬到何处使得他走邻居路程最短,其实就是求中位数.首先,我讲所输入的若干个数字进行排序(数字输入前,还有其他的输入,案例个数,要输入的数字个数),我采用的是冒泡,紧接着,就是找出中位数,(奇数的中位数就是中间的那个数,而偶数的中位数是中间的那2个数的平均数.),找到中位数后,将其与各个数的差相加即可.
#include"stdio.h"int main(void){    int t,n,i,j,swap,temp,s,sum,b;    int a[30000];    scanf("%d",&t);    while(t--)    {   sum=0;        scanf("%d",&n);        for(i=0;i

 

转载于:https://www.cnblogs.com/sjy123/archive/2013/02/23/2923620.html

你可能感兴趣的文章
关于共享SQL——窥视解析
查看>>
linux和windows同步数据 cwrsync client to rsync server
查看>>
浅谈ListBox在Windows Phone 7 中的使用
查看>>
ECMAScript 6 -- 字符串的扩展
查看>>
android apk--程序发布前的准备
查看>>
现任明教教主CCNP Security Secure第四天第三部分
查看>>
SCCM 2012系列3 安装SCCM 2012
查看>>
跟小菜学虚拟化,之Microsoft Hyper-v 学习笔记(一)
查看>>
静态数据成员
查看>>
继LINQ动态组合查询PredicateExtensions讲解
查看>>
使用zabbix discovery监控网卡百兆
查看>>
mysql连接小错误一例
查看>>
Flash持续集成自动化单元测试
查看>>
oracle一点资料
查看>>
28个实用的源码/文档比较合并工具
查看>>
js的cookie操作
查看>>
Nginx 域名跳转配置
查看>>
ASP.NET MVC扩展库
查看>>
pyodbc简单使用
查看>>
数据库厂商提供的 Providers for ASP.NET
查看>>