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;
    }
    
  • 相关阅读:
    MyBatis Generator 的使用
    Eclipse插件手动安装
    MyBatis中别名的设置
    MyBatis 插入时返回自增主键
    MyBatis 模糊查询
    MyBatis 多个查询条件的传递
    MyBatis 的Mapper中有小于号的处理
    spring mvc rest 方式
    jackson
    中兴阅读:你的移动阅读解决专家,助纸媒们一臂之力
  • 原文地址:https://www.cnblogs.com/houzm/p/12258632.html
Copyright © 2011-2022 走看看