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;
}

 

posted on 2013-02-08 10:41  喂喂还债啦  阅读(805)  评论(0编辑  收藏  举报