购物
来源:互联网 发布:mysql的left方法 编辑:程序博客网 时间:2024/04/27 18:15
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
424 13 89 3767 30 41 14 39 420
Sample Output
15270
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;
}
- 购物
- 购物
- 购物
- 购物
- 购物卷购物问题
- 购物车
- 疯狂购物
- 购物车
- 购物狂
- 购物车
- 购物篇
- 大光节购物
- 购物车
- 比较购物
- Shopping 购物
- 商店购物
- 购物车
- 购物车
- lua加载文件 读取数据到C
- DES 算法 信息论与编码
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- Python学习笔记(六)----Python条件语句
- Linux系统基础学习之shell及常用命令(二)
- 购物
- 基础的快速排序
- NSIndexPath类的说明
- CentOS 6.4 下用ntfs-3g挂载Windows NTFS分区
- 结构体与共用体
- 入职第一天
- 零基础GitHub入门教程
- NYOJ 65 另一种阶乘问题
- J2EE入门必备