⬆︎
×

[PAT-A] 1008 Elevator

Hyplus目录

Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        int res = 0, cur = 0;
        while (n-- > 0) {
            int x = scanner.nextInt();
            if (cur - x < 0) {
                res += (x - cur) * 6 + 5;
            } else {
                res += (cur - x) * 4 + 5;
            }
            cur = x;
        }

        System.out.println(res);
    }
}

C++

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    int n;
    cin >> n;

    int res = 0, cur = 0;
    while (n--) {
        int x;
        cin >> x;
        if (cur - x < 0) res += (x - cur) * 6 + 5;
        else res += (cur - x) * 4 + 5;
        cur = x;
    }

    cout << res << endl;

    return 0;
}

发表评论