PAT (Advanced Level) 1008 Elevator (20 分) 【Python/C++】【模拟】

博客提及PAT(Advanced Level)1008 Elevator题目,分值20分,还强调连续两人在同一层需停留5秒,给出了Python和C++两种语言的解答相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PAT (Advanced Level) 1008 Elevator (20 分)

注意:连续两个人都处于同一层,仍需要停留5秒。

Python:

import sys
str_in = input()
a = [int(n) for n in str_in.split()]
#print(a)
n = int(a[0])
res = int(0)
ans = int(0)
for i in range(1, n + 1):
    x = int(a[i])
    if res > x:
        ans += 4 * (res - x) + 5
    elif res < x:
        ans += 6 * (x - res) + 5
    else:
        ans += 5
    res = x
print(ans)



C++:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
const int maxn = 1e5 + 10;
int main()
{
	int n;
	scanf("%d", &n);
	int res = 0;
	int ans = 0;
	int x;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &x);
		if (res < x) {
			ans += 6 * (x - res);
		}
		else if (res > x) {
			ans += 4 * (res - x);
		}
		ans += 5;
		res = x;
	}
	cout << ans << endl;
	return 0;
}
//_CRT_SECURE_NO_WARNINGS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值