zoukankan      html  css  js  c++  java
  • Codewars Solution:Number of People in the Bus

    Level 7kyu :Number of People in the Bus

    城里有辆公共汽车在行驶,每个公共汽车站都载有一些人上下车。

    为您提供了一个整数数组(或元组)的列表(或数组)。每个整数数组都有两个项目,分别代表公交车站的乘车人数(第一项)和下车的人数(第二项)。

    您的任务是返回上一个公共汽车站之后(最后一个数组之后)仍在公共汽车中的人数。

    即使是最后一个公共汽车站,公共汽车也不是空的,有些人仍在公共汽车上,他们很可能在那睡觉:D

    看一下测试用例。

     1 public class MetroTest {
     2     Metro metro = new Metro();
     3     @Test
     4     public void test1() {
     5     ArrayList<int[]> list = new ArrayList<int[]>();
     6     list.add(new int[] {10,0});
     7     list.add(new int[] {3,5});
     8     list.add(new int[] {2,5});
     9     assertEquals(5, metro.countPassengers(list));
    10     }
    11 }

    请记住,测试用例确保总线上的人数始终> =0。因此返回整数不能为负。

    第一个整数数组中的第二个值为0,因为总线在第一个公共汽车站中为空。

    主要方法:

    size()->获取ArrayList变量的长度

    get(索引)->获取ArrayList的元素

     1 import java.util.ArrayList;
     2 class Metro {
     3   public static int countPassengers(ArrayList<int[]> stops) {
     4   //Code here!
     5     int begin=0;//公交车人数,初始为0
     6     for(int i=0;i<stops.size();i++){
     7       begin=begin-stops.get(i)[1]+stops.get(i)[0];
     8     }
     9     return begin;
    10   }
    11 }
  • 相关阅读:
    Qt高级编码约定
    QTextCodec::codecForLocale
    无边框窗口拖动代码
    QString QByteArray char 之间的转换
    Qt 程序发布指南
    qt demo pro
    snort vtun performance on vm
    xhEditor用法
    MVC控制器使用总结
    第七章 模态框
  • 原文地址:https://www.cnblogs.com/mc-web/p/12940254.html
Copyright © 2011-2022 走看看