比例表示和比率

时间:2015-05-19 04:37:45

标签: arrays sqf

我需要生成一个数字列表以放入数组中。它们在列表中出现的次数取决于某个属性的重要程度。

E.g。

// importance ranges from 0 (no importance) to 5 (utmost importance)
maxNumbersInArray = 10;

propAImportance = 5; --> will put 1 into the array
propBImportance = 3; --> will put 2 into the array
propCImportance = 2; --> will put 3 into the array

// Output will be
array = [1,1,1,1,1,2,2,2,3,3]

如何创建一个列表,其中值的重要性意味着将值添加到数组的次数更多,或者更具体地说,如何计算如何根据属性将值添加到数组中重要性

第一个想法:(语言SQF脚本)

_max = 10;

_weight1 = 3;
_weight2 = 2;
_weight3 = 0;
_weight4 = 1;

_weights = [_weight1,_weight2,_weight3,_weight4];

_arr = [];

for "_i" from 0 to (_max - 1) do {
   {
      while {_i <= _x} do {
      _arr set [(count _arr),_forEachIndex];
   } forEach _weights;
};

_arr; // Output

1 个答案:

答案 0 :(得分:0)

我相信这是要使用的算法:

_index = [];
_max = 10;
_weights = [5,2,1,1];
_count = 0;
_countWeight = 0;
while {(count _index ) < _max } do {
   if (_count >=  (count _weights)) then {_count = 0};
   _weight = _weights select _count;
   _countWeight = _countWeight + 1;
   if (_countWeight > _weight) then {
      _countWeight = 0;
      _count = _count + 1;
   } else {
      _index set [(count _index ),_count];
   };
};