dongme8388 2013-10-23 16:32
浏览 95
已采纳

数组中元素的梯度分布

this may be a dumb question,so sorry in advance.

I need to distribute 2 kinds of elements in an array, like in a gradient (is the image that comes to mind).

I'll try to explain (sorry again, I don't know some terms).

I have an array of X,Y objects, I'd like to arrange them like this (array.length will be fixed)

[X,X,X,Y,X,Y,X,Y,Y,Y] - in the middle, when X is disappearing , y start to show, and I'm always sure that array[0] is always X , and array[array.lenght-1] is always Y.

How to do this ?

Sorry again if the question isn't so clear.

  • 写回答

1条回答 默认 最新

  • duanchen1937 2013-10-25 14:16
    关注

    This isn't perfect (not even slightly), but it might meed your needs, at the very least it might give you some inspiration:

    <?php
    $Class1 = 'a';
    $Class2 = 'b';
    
    print_r(gradient($Class1, $Class2, 10));
    
    function gradient($class1, $class2, $size){
        $result = array();
    
        for($i=0; $i<$size; $i++){
            $result[$i] = rand(0, 99) >= $i/$size*100 ? $class1 : $class2;
        }
    
        return $result;
    }
    ?>
    

    Sample output (higher sizes will make it closer to your example):

    Array ( [0] => a [1] => a [2] => a [3] => a [4] => b [5] => b [6] => a [7] => b [8] => b [9] => a ) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料