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;
}