Shopping
来源:互联网 发布:竖琴 知乎 编辑:程序博客网 时间:2024/05/23 14:06
描述
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 integerN (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.
样例输入
24 13 89 37
6
7 30 41 14 39 42
0
样例输出
70
#include <iostream> #include <cstring> #include <cstdio> #include <string> #include <algorithm> #include <cmath> #define MAX 100005 #define LL long long using namespace std; int main() { int n; int a[MAX]; while(~scanf("%d",&n)&&n!=0){ for(int i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); LL sum=0; for(int i=1;i<n;i++) sum+=(a[i]-a[i-1]); sum+=(a[n-1]-a[0]); printf("%lld\n",sum); } return 0; }
- shopping
- shopping
- Shopping
- Shopping
- shopping
- Shopping
- Shopping 购物
- Shopping feast
- shopping list
- Shopping Offers
- [ACM]Shopping
- Shopping Offers
- HK shopping
- codeforces_665B. Shopping
- Beg-Shopping
- [bzoj4182]Shopping
- 4202. Shopping
- Shopping CodeForces
- Linux基本命令的使用及目录权限问题
- Object 类中的 equals方法
- 阿里面试问题(转牛客网上分享的题目)
- dom4j遍历xml文件
- C++之中的友元函数的作用
- Shopping
- 窗口分析函数
- 参数—传值调用
- 远程使用x-manager中Xstart工具报缺少xterm包错误
- OpenCV-Python learning---1
- JavaFX应用程序的基本骨架
- Jackson介绍
- 蓝桥杯 算法提高 三进制数位和
- Android函数响应式编程——RxJava不完整定义回调,更灵动的观察者写法