连续合格次数 js算法

2019-08-13  本文已影响0人  九月_adhoc
  onSecnename: function() {
    var array = []
   
    for (var i = 0; i < 100000000 ; i++) {
      var s = ""
      var res = 0
      for (var j = 0; j < 5; j++) {
        var a = Math.floor(Math.random() * 10)
        s += a.toString()
        res += a
      }
     
      let ge = parseInt(s.substr(4, 1))

      let obj = { num: s, des: res > 22 ? 1 : 0}   // 0 -  45   

      // let obj = { num: s, des: ge >4 ? 1 : 0 }   // 0 -  45   

      array.push(obj)
    }
    console.log(array)

    var count_0 = 0;   //不合格次数
    var count_1 = 0;   //合格次数
    var max_0 = 0;   //最大连续不合格次数
    var max_1 = 0;   //最大连续合格次数

    for (var i = 0; i < array.length; i++) {
      let obj  = array[i]
      
      if (obj.des==0) {
        count_0++;   //如果有不合格的,需要把累计的合格次数清零
        count_1 = 0;
      } else if (obj.des == 1) {
        count_1++;
        count_0 = 0;
      }
      if (count_0 > max_0) {
        max_0 = count_0;
      }
      if (count_1 > max_1) {
        max_1 = count_1;
        console.log("pre",max_1,i)
      }
    }

    console.log("max1", max_1)

    // console.log("max0", max_0)



  }
上一篇下一篇

猜你喜欢

热点阅读