zoukankan      html  css  js  c++  java
  • PAT Advanced 1008 Elevator (20) [数学问题-简单数学]

    题目

    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.
    Input Specification:
    Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.
    Output Specification:
    For each test case, print the total time on a single line.
    Sample Input:
    3 2 3 1
    Sample Output:
    41

    题目分析

    已知要到达的N个楼层数,初始楼层为0,上一层楼需6s,下一层楼需4s,每一层楼停靠5s,计算总耗时

    解题思路

    依次遍历楼层号,停靠时间可统一计算N*5s,也可以在每层楼进行累加

    • 若当前楼层号>上个楼层号,上楼,总耗时+=6s。每层楼停靠5s;
    • 若当前楼层号<上个楼层号,下楼,总耗时+=4s。每层楼停靠5s;

    思路01(最优)

    指针记录上个楼层的楼层号

    思路02

    数组记录每个楼层的楼层号

    Code

    Code 01

    #include <iostream>
    using namespace std;
    int main(int argc,char * argv[]) {
    	int n;
    	scanf("%d",&n);
    	int t=n*5;
    	int cf,pf = 0;//cf 当前楼层,pf起始楼层0
    	for(int i=1; i<=n; i++) {
    		scanf("%d",&cf);
    		if(cf>pf) t+=(cf-pf)*6;
    		else t+=(pf-cf)*4;
    		pf = cf;
    	}
    	printf("%d",t);
    	return 0;
    }
    

    Code 02

    #include <iostream>
    using namespace std;
    int main(int argc,char * argv[]) {
    	int n;
    	scanf("%d",&n);
    	int f[n+1]= {0}; //下标0处为哨兵,起始楼层0
    	int t=n*5;
    	for(int i=1; i<=n; i++) {
    		scanf("%d",&f[i]);
    		if(f[i]>f[i-1]) t+=(f[i]-f[i-1])*6;
    		else t+=(f[i-1]-f[i])*4;
    	}
    	printf("%d",t);
    	return 0;
    }
    
  • 相关阅读:
    PHP array_udiff_assoc() 函数
    PHP array_udiff() 函数
    app已损坏,打不开。你应该将它移到废纸篓
    [WC2013]糖果公园
    win10 uwp 绑定静态属性
    win10 uwp 修改Pivot Header 颜色
    win10 uwp 修改Pivot Header 颜色
    win10 uwp 修改Pivot Header 颜色
    win10 uwp 反射
    win10 uwp 反射
  • 原文地址:https://www.cnblogs.com/houzm/p/12258632.html
Copyright © 2011-2022 走看看