zoukankan      html  css  js  c++  java
  • sdutoj 2154 Shopping

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154

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

    示例程序

    分析:

    求路径和,直接求最值差的二倍即可。

    AC代码:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int a[100005];
     6 int main()
     7 {
     8     int n;
     9     while(cin>>n&&n)
    10     {
    11         memset(a,0,sizeof(a));
    12         for(int i=0;i<n;i++)
    13         {
    14             cin>>a[i];
    15         }
    16         sort(a,a+n);
    17         cout<<2*(a[n-1]-a[0])<<endl;
    18     }
    19     return 0;
    20 }
    View Code
  • 相关阅读:
    web应用后台开发的故事
    XML的定义、用途、以及它的发展前景和存在的问题等等
    本学期(大三下学期)学习目标
    企业级应用与互联网应用的区别?
    新能源汽车无线充电管理网站4
    新能源汽车无线充电管理网站3
    新能源汽车无线充电管理网站2
    企业级应用与互联网应用的区别
    javaee 新学期新目标
    团队项目PCP--自我评价
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4468381.html
Copyright © 2011-2022 走看看