算法7:公共汽车到终点站后没下车的乘客人数

 *公共汽车上的人数

    *城市里有一辆公共汽车在行驶,每一个公共汽车站都要接送一些人。

    *将为您提供整数数组(或元组)的列表(或数组)。每个整数数组有两个项,分别表示公共汽车站上车人数(第一项)和下车人数(第二项)。

    *您的任务是返回最后一个公共汽车站(最后一个数组之后)之后仍在公共汽车上的人数。即使这是最后一个公共汽车站,公共汽车也不是空的,有些人还在车上,他们可能睡在那里。

    *看看测试用例。

    *请记住,测试用例确保公共汽车中的人数始终大于等于0。所以返回的整数不能为负。

    *第一个整数数组中的第二个值为0,因为第一个公共汽车站中的公共汽车为空(才开始上乘客,没有下车的乘客)。

  

    public static int countPassengers(ArrayList<int[]> stops) {
        //Code here!
        ArrayList<int[]> list = new ArrayList<int[]>();

        int persionNum = stops.get(0)[0];// the bus persion number of the first stop
        persionNum = persionNum + stops.get(1)[0] - stops.get(1)[1];
        persionNum = persionNum + stops.get(2)[0] - stops.get(2)[1];

        System.out.println(persionNum);
        return persionNum;
    }

    public static void main(String[] args) {
        ArrayList<int[]> list = new ArrayList<int[]>();
        //整数数组list,每个整数数组有两个项,分别表示公共汽车站上车人数(第一项)和下车人数(第二项)。
        list.add(new int[]{10, 0});
        list.add(new int[]{3, 5});
        list.add(new int[]{2, 5});

        countPassengers(list);
    }
结果:5

  

原文地址:https://www.cnblogs.com/bors/p/countPassengers.html