把几个数字拆分保存的算法有关问题

WBOY
Release: 2016-06-13 10:50:44
Original
827 people have browsed it

把几个数字拆分保存的算法问题
假如有五个数字:1,2,3,4,5,要保存到数组中,结果如下:

数组1:2,3,4,5
数组2:1,3,5
数组3:1,2,3,4,5
数组4:1,2,4

一共有4个数组,我的目的是把这5个数字在这四个数组中每个数字都出现3次,这样有一个数组不存在了,从其它数组也能把所有数字取出来。

我不需要代码,只要有思路就行,谢谢各位达人。





------解决方案--------------------
想了一下,你这还不是 raid5.你的这个方案的冗余度要大的多
象这样组织数据的话,可以在丢失任意两组数据的情况下,读出数据。
不过组织方式与你的略有不同

PHP code
$ar = array(  array(0,2,3,4,5),  array(1,0,3,0,5),  array(1,2,3,4,5),  array(1,2,0,4,0),);unset($ar[3]);unset($ar[1]);for($i=0; $i<font color="#e78608">------解决方案--------------------</font><br>
Copy after login
PHP code
<?phpfunction split2Array($n, $repeat = 3){    $result = array();    for($i = 0; $i < $n; $i++)    {        for($j = 0; $j < $repeat; $j++)        {            $result[($i + $j) % ($repeat + 1)][$i] = $i + 1;        }    }    return $result;}print_r(split2Array(7, 4));?><div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!