Elevator

来源:互联网 发布:mac上能玩的网游 编辑:程序博客网 时间:2024/06/05 23:39

Problem Description
The highest building in our city has only one elevator. A request list is made up with N positive
numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6
seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator
will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list.
The elevator is on the 0th floor at the beginning and does not have to return to the ground floor
when the requests are fulfilled.
10
Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive
numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of
input. This test case is not to be processed.
Output
Print the total time on a single line for each test case.

#include <iostream>using namespace std;void sort (int *p);int i;int main (){    int table[110]={0};    int count = 0, second;    i = 0;    do    {        cin >> table[i];        i++;    }while(table[i-1]!=0);    sort(table);    for(int j = 0; j < i-1; j++)    {        if(table[j]!=table[j+1])            count++;    }    second = 6*table[i-2] + 4*(table[i-2]-table[0]) + 5*(count);    cout << second << endl;    return 0;}void sort (int *p){    int temp;    for(int j = 0; j < i-1; j++)    {        for(int k = 0; k < i-j-2; k++)        {            if(*(p+k) > *(p+k+1))            {                temp = *(p+k);                *(p+k) = *(p+k+1);                *(p+k+1) = temp;            }        }    }}

原创粉丝点击