JavaScript检索垂直列获胜者

时间:2019-04-05 07:46:48

标签: javascript

我有一个名为Columns的游戏,您可以在其中押注80号矩阵的10个垂直列之一将收集20个抽取数字中的大部分。 没有抽奖。如果是平局列,则获胜列是其数字首先被抽取的列。

我需要一种算法来根据绘制的数字计算哪些列获胜

示例

80 Matrix 
Col1    Col2    Col3    Col4    Col5    Col6    Col7    Col8    Col9    Col10
1   2   3   4   5   6   7   8   9   10
11  12  13  14  15  16  17  18  19  20
21  22  23  24  25  26  27  28  29  30
31  32  33  34  35  36  37  38  39  40
41  42  43  44  45  46  47  48  49  50
51  52  53  54  55  56  57  58  59  60
61  62  63  64  65  66  67  68  69  70
71  72  73  74  75  76  77  78  79  80


First Extract number = [22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,21]
the winner is Col2

Second Extract number = [1,12,23,31,15,42,16,27,18,39,43,71,25,56,35,55,57,58,65,17]
The Winner is Col5

我使用了以下代码

number = [22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,21]

Matrix3 = [ {"Col":0,"selNum":0,"weight":0},
            {"Col":1,"selNum":0,"weight":0},
            {"Col":2,"selNum":0,"weight":0},
            {"Col":3,"selNum":0,"weight":0},
            {"Col":4,"selNum":0,"weight":0},
            {"Col":5,"selNum":0,"weight":0},
            {"Col":6,"selNum":0,"weight":0},
            {"Col":7,"selNum":0,"weight":0},
            {"Col":8,"selNum":0,"weight":0},
            {"Col":9,"selNum":0,"weight":0}
    ];


for (i=0;i<20;i++)
    {
        Matrix3[((number[i]).toString()).slice(-1)].selNum = Matrix3[((number[i]).toString()).slice(-1)].selNum +1;
        Matrix3[((number[i]).toString()).slice(-1)].weight = Matrix3[((number[i]).toString()).slice(-1)].weight +i;


    }


console.log(Matrix3);

现在从Matrix3中我需要检查:

  • 如果selNum上没有重复项,则返回最大selNum的Col

  • 如果重复项具有相同的最大selNum,则从权重较低的重复项中返回Col

2 个答案:

答案 0 :(得分:0)

如果您的矩阵具有类似1, 2, 3, 4, ...的数字结构,则可以使用 remainder operator 来了解哪个列值代表。请参见下面的代码。希望这会有所帮助。

const COLUMNS_AMOUNT = 10

const getWinnerCol = (numbers) => {
  const columnsHits = new Array(COLUMNS_AMOUNT).fill(0)
  
  for (const number of numbers) {
    const columnHit = (number % COLUMNS_AMOUNT) || COLUMNS_AMOUNT
    
    columnsHits[columnHit - 1] += 1
  }
  
  console.log('columnsHits', columnsHits)
  
  return columnsHits.indexOf(Math.max(...columnsHits))
}

const firstNumbers = [22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,21],
      secondNumbers = [1,12,23,31,15,42,16,27,18,39,43,71,25,56,35,55,57,58,65,17]

console.log('Winner column index:', getWinnerCol(firstNumbers))
console.log('Winner column index:', getWinnerCol(secondNumbers))

答案 1 :(得分:0)

这是通过在按顺序列出列条目时跟踪获胜者来解决的(有关详细信息,请参见代码中的注释):

const nums = [22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,21];
const cols = nums.map( (num) => [num % 10]); // takes the last digit
let counts = {}, max = 0, winner = 0;

for (let col of cols){
  counts[col] = counts[col] ? counts[col] + 1 : 1; // adds 1 to the count for this column
  if(counts[col] > max){ // we have found a new maximum
    max = counts[col];
    winner = col > 0 ? col : 10; // updates winner (uses 10 if last digit is 0)
  }
}
console.log(winner);