11364 - Parking

When shopping on Long Street, Michael usually parks his car at some random location, and then walks to the stores he needs. Can you help Michael choose a place to park which minimises the distance he needs to walk on his shopping round?

\epsfbox{p11364.eps} <TEX2HTML_VERBATIM_MARK>

Long Street is a straight line, where all positions are integer. You pay for parking in a specific slot, which is an integer position on Long Street. Michael does not want to pay for more than one parking though. He is very strong, and does not mind carrying all the bags around.

Input 

The first line of input gives the number of test cases, 1$ \le$t$ \le$100 <TEX2HTML_VERBATIM_MARK>. There are two lines for each test case. The first gives the number of stores Michael wants to visit, 1$ \le$n$ \le$20 <TEX2HTML_VERBATIM_MARK>, and the second gives their n <TEX2HTML_VERBATIM_MARK>integer positions on Long Street, 0$ \le$xi$ \le$99 <TEX2HTML_VERBATIM_MARK>.

Output 

Output for each test case a line with the minimal distance Michael must walk given optimal parking.

在长龙街上购物时,麦可通常在路边随便找个地方停车,然后走路去店里。你可以帮麦可找个停车位,好让他所走的距离最短吗?

长龙街是一条直线,所有的店家及收费停车位都在直线的整数位置上。麦可要去很多家店,但是只想付一次的停车费。他很强壮,不在乎提着很多袋子走路。

Input

输入的第一行代表测试笔数1<= t <= 100,每笔测试有两行。第一行是麦可要去几家店1<= n <= 20,第二行则是n 个店家在长龙街上的整数位址0 <= xi <= 99。请参考Sample Input。

Output

每笔测试要输出一行,印出如果车停得好的话麦可最少需要行走的距离。

Sample Input 

2 
4 
24 13 89 37 
6 
7 30 41 14 39 42

Sample Output 

152
70

解题思路:计算这几个数中差的最大值,再乘以2

#include<stdio.h>
int main()
{int t,n,i,j,b;
scanf("%d",&t);
while(t--){
           scanf("%d",&n);
           if(n>0){
                   int x[20]={0};
                   for(i=0;i<n;i++)
                   scanf("%d",&x[i]);
                   for(i=0;i<n-1;i++)
                   for(j=i+1;j<n;j++)
                   if(x[j]>x[i]){b=x[i];x[i]=x[j];x[j]=b;}
                   printf("%d\n",(x[0]-x[n-1])*2);
                   }
           else if(n==0)printf("0\n")
           }
return 0;
}

 

转载于:https://www.cnblogs.com/EVA00/archive/2013/02/08/2909162.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值