尝试遍历数组并记住上一个位置

时间:2019-02-04 10:45:53

标签: javascript

我有一点任务无法完成。

*目标 您最近已被分配到新游乐园的分析和监管中心。您的任务是估计每天每一天的收益。您首先看过山车。

规则 您会注意到人们非常喜欢过山车,以至于他们骑完车后便不由自主地回去了。 人们在景点前排队 他们可以是一个人或一个人。当小组在队列中时,他们必然希望一起骑行而不会被分开。 人们从不会在队列中互相超越。 当景点中没有足够的空间容纳队列中的下一组游客时,游乐设施将开始行驶(因此乘驾并不总是满载的)。 骑行结束后,出现的组将以相同的顺序返回队列。*

    var limited_number_of_seats_per_ride   = 3, //L
        limited_number_of_turns_per_ride   = 3, //C
        limited_number_of_groups_in_queue  = 4, //N
        number_of_person_per_group         = [3,1,1,2]; //P

   function earning_per_day(P,L,C,price){

      /**
      *
      * @P              {array}      number of person per group.
      * @L              {int}        limited number of seats per ride.
      * @C              {int}        limited number of times the ride goes per day.
      * @price          {int}        price per person per ride.
      *
      * @daily_earning {int}         estimated daily earnings.
      */

      var daily_earning = 0;
      var total_per_ride = 0;
      // calculates sum of the earning for each ride per day
      for (var j = 0; j < C; j++) {
        total_per_ride = 0;
        position_i = 0;
        // calculates the number of person on each ride
        for (var i = position_i; i < P.length; i++){
          while((total_per_ride + P[i+1]) < L){
            total_per_ride += P[i];
            console.log(i + ' total_per_ride = ' + total_per_ride);
          }
          position_i = i;
          console.log('position_i = ' + position_i);
        }
        daily_earning += total_per_ride;
        console.log('daily_earning ' + daily_earning);
      }
    }

    // calling
    earning_per_day(number_of_person_per_group, limited_number_of_seats_per_ride, limited_number_of_turns_per_ride, 1);

这是我到目前为止所拥有的。但是,我有一个想法,我无法设法落实。由于规则指出 “人们非常喜欢过山车,以至于一旦骑完车,他们便忍不住又回去了。” 。我想在循环结束时保存循环的位置,然后在该完全相同的位置重新启动循环。避免我将元素推并移动到数组的最后位置。

有人可以给我提示如何进行吗?

非常感谢!

瑞安

1 个答案:

答案 0 :(得分:0)

我几乎没有更新您的逻辑。

为了连续循环P,我将增量索引position_i用作position_i = (position_i + 1) % P.length;

不需要内部for (var i = position_i; i < P.length; i++){。刚刚更新了while(){...}

var limited_number_of_seats_per_ride = 3, //L
  limited_number_of_turns_per_ride = 3, //C
  limited_number_of_groups_in_queue = 4, //N
  number_of_person_per_group = [3, 1, 1, 2]; //P

function earning_per_day(P, L, C, price) {

  /**
   *
   * @P              {array}      number of person per group.
   * @L              {int}        limited number of seats per ride.
   * @C              {int}        limited number of times the ride goes per day.
   * @price          {int}        price per person per ride.
   *
   * @daily_earning {int}         estimated daily earnings.
   */

  var daily_earning = 0;
  var position_i = 0;
  console.log('---daily_earning : ' + daily_earning + '---');

  // calculates sum of the earning for each ride per day
  for (var j = 0; j < C; j++) {
  console.log('---Ride ' + (j + 1) + '---');
    var total_per_ride = 0;
    // calculates the number of person on each ride
    // for (var i = position_i; i < P.length; i++){
    while ((total_per_ride + P[position_i]) <= L) {
      total_per_ride += P[position_i];    
      position_i = (position_i + 1) % P.length;
    }
    console.log('total_per_ride = ' + total_per_ride);
    console.log('New Start position_i = ' + position_i);
    // }
    daily_earning += total_per_ride;
    console.log('daily_earning ' + daily_earning);
  }
}

// calling
earning_per_day(number_of_person_per_group, limited_number_of_seats_per_ride, limited_number_of_turns_per_ride, 1);