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.
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.
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.
示例输入
4 24 13 89 37 6 7 30 41 14 39 42 0
示例输出
152 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大学生程序设计竞赛
链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154
水题 排序
#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; } |
分类:
杂题水题
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
· Tinyfox 简易教程-1:Hello World!
2015-04-25 蓝桥杯---最大子阵
2015-04-25 无穷大技巧设置
2015-04-25 蓝桥杯---蚂蚁感冒(水题)