Shopping

野性酷女 2022-08-18 02:35 262阅读 0赞

Shopping

#

Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^

题目描述

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.

输入

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.

输出

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

示例输入

  1. 4
  2. 24 13 89 37
  3. 6
  4. 7 30 41 14 39 42
  5. 0

示例输出

  1. 152
  2. 70

提示

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

来源

2010年山东省第一届ACM大学生程序设计竞赛

示例程序

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int cmp(const void *a,const void *b)
  4. {
  5. return *(int *)a-*(int *)b;
  6. }
  7. int a[100010];
  8. int main()
  9. {
  10. int i,j,n,m,k,t;
  11. while(scanf("%d",&n)&&n)
  12. {
  13. for(i=0;i<n;i++)
  14. scanf("%d",&a[i]);
  15. qsort(a,n,sizeof(a[0]),cmp);
  16. k=0;
  17. for(i=1;i<n;i++)
  18. k+=a[i]-a[i-1];
  19. k+=a[n-1]-a[0];
  20. printf("%d\n",k);
  21. }
  22. }

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java实战——落饼Shop(WEB版)

    我终于想起还有CSDN这个东西,来丰富一波为数不多的公主号代码库~ 今天带来的是WEB版的小商城(PS:GZH里头有前端的完整代码提供,下面讲的东西包含后端(继续PS:清空购