首页 > 其他 > 详细

山东省第一届省赛重现 G:Shopping

时间:2014-04-13 20:59:27      阅读:588      评论:0      收藏: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.

示例输入

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大学生程序设计竞赛

 

  水题,最大值减最小值的差乘以二就是结果。

  代码:

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int n;
 8     int a[100010];
 9     while(scanf("%d",&n)!=EOF){
10         if(n==0) break;
11         long long Max=0,Min=9999999999;
12         for(int i=1;i<=n;i++){
13             scanf("%d",&a[i]);
14             if(a[i]>Max)    //找到最大值
15                 Max = a[i];
16             if(a[i]<Min)    //找到最小值
17                 Min = a[i];
18         }
19         long long sum =(Max-Min)*2;
20         cout<<sum<<endl;
21     }
22 
23     return 0;
24 }
bubuko.com,布布扣

 

Freecode : www.cnblogs.com/yym2013

山东省第一届省赛重现 G:Shopping,布布扣,bubuko.com

山东省第一届省赛重现 G:Shopping

原文:http://www.cnblogs.com/yym2013/p/3662184.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!