PAT A1008 Elevator

PAT A1008 Elevator

在这里插入图片描述

Sample Input:

3 2 3 1

Sample Output:

41
  • 思路 1:
    用一个ans累加结果,每次输入都和上次输入做比较(用一个pre,每轮循环处理完ans后更新),上楼:+楼差6 ; 下楼:+楼差4;一样:什么也不做,不论上楼还是下楼还是什么都不做:都要+5

  • code 1:

#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
	int n, next, pre = 0, ans = 0;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &next);
		if(next > pre) ans += (next - pre) * 6; //上楼
		else if(next < pre) ans += (pre - next) * 4;	//下楼
//		else ans += 5;	//和下面的+5重复,如果请求相同就多+了5
		ans += 5; 
		pre = next;
	}
	printf("%d", ans); 
	return 0;
}
  • 注意!!!: 别忘了N!!!
    每次都被这个N骗了,注意输入为序列时,第一个是N(数组长度),不是第一个元素!!

  • T2 code:

#include <bits/stdc++.h>
using namespace std;

int main(){
	int n, pre = 0, next, sum = 0;
	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &next);
		int d = next - pre;
		sum += d > 0 ? d * 6 : d * -4;
		sum += 5;
		pre = next;
	}
	printf("%d", sum);
//cnmd,每次都被这个N骗了,注意输入为序列时,第一个是N(数组长度),不是第一个元素!! 
//cout << 2 * 6 + 5 + 1 * 6 + 5 + 2 * 4 + 5;
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值