購物

D - Shopping
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu

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

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

代碼:#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[10000000];
int main()
{
    int n,i,sum;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        memset(a,0,sizeof(a));
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        sum=0;
        for(i=1; i<n; i++)
            sum+=(a[i]-a[i-1]);
        printf("%d\n",sum+a[n-1]-a[0]);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章