D - Shopping

Description

Saya and Kudo go shopping together.
You can assume the street as a straight line, while the shops are some points on the line.
They park their car at the leftmost shop, visit all the shops from left to right, and go back to their car.
Your task is to calculate the length of their route.

Input

The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N<100001), represents the number of shops.
The next line contains N integers, describing the situation of the shops. You can assume that the situations of the shops are non-negative integer and smaller than 2^30.
The last case is followed by a line containing one zero.

Output

 For each test case, print the length of their shopping route.

Sample Input

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

Sample Output

152
70

Hint

Explanation for the first sample: They park their car at shop 13; go to shop 24, 37 and 89 and finally return to shop 13. The total length is (24-13) + (37-24) + (89-37) + (89-13) = 152

 

解题思路:

因为从最近商店出发,到达最远商店然后回来,所以距离为最远商店坐标减去最近商店坐标乘2

代码:

#include <iostream>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int n,i,c;
    vector<int>a;
    while(cin>>n)
    {
        if(n==0) break;
        for(i=0;i<n;i++)
        {cin>>c;a.push_back(c);}
        sort(a.begin(),a.end());
        int sum=2*(a[n-1]-a[0]);
        cout<<sum<<endl;
        a.clear();
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值