欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

急.数字重组计算问题

程序员文章站 2024-02-07 12:16:04
...
问题描述:
现在有一组数字需要计算重组后总数,以最快的速度计算出总数.
数字不一定就是给出的这些,也有可能是其它数字
数字:
例一
1->0123
2->01234
3->678
总数:60


例二
1->0123456789
2->0123456789
3->01234
总数:500


写一个方法计算出重组后的总数和重组后的号码


回复讨论(解决方案)

不明白你的意思
最好给个样例

看不懂~~~~

样例:

1->0
2->01
3->01

重组后的号码为000,001,010,011,总数为4

这个意思?

$ar = array('0123','01234','678',);print_r(foo($ar));function foo(&$ar, $i=0) {  $res = array();  if($i+1 Array  
(
[0] => 006
[1] => 007
[2] => 008
[3] => 016
[4] => 017
[5] => 018
[6] => 026
[7] => 027
[8] => 028
[9] => 036
[10] => 037
[11] => 038
[12] => 046
[13] => 047
[14] => 048
[15] => 106
[16] => 107
[17] => 108
[18] => 116
[19] => 117
[20] => 118
[21] => 126
[22] => 127
[23] => 128
[24] => 136
[25] => 137
[26] => 138
[27] => 146
[28] => 147
[29] => 148
[30] => 206
[31] => 207
[32] => 208
[33] => 216
[34] => 217
[35] => 218
[36] => 226
[37] => 227
[38] => 228
[39] => 236
[40] => 237
[41] => 238
[42] => 246
[43] => 247
[44] => 248
[45] => 306
[46] => 307
[47] => 308
[48] => 316
[49] => 317
[50] => 318
[51] => 326
[52] => 327
[53] => 328
[54] => 336
[55] => 337
[56] => 338
[57] => 346
[58] => 347
[59] => 348
)

如果不考虑重复问题,这个不就是排列组合了?
1->0123
2->01234
3->678
总数 = A(4,1)*A(5,1)*A(3,1) = 4*5*3 = 60

如果不考虑重复问题,这个不就是排列组合了?
1->0123
2->01234
3->678
总数 = A(4,1)*A(5,1)*A(3,1) = 4*5*3 = 60
代码?

echo strlen('0123')*strlen('01234')*strlen('678');//??这个算最快不?



噢!只要计算组合数啊?!我还以为要求所有的组合呢

$ar = array('0123456789', '0123456789', '01234');echo array_product(array_map('strlen', $ar));

其实你们的方法都是对的..只是我没有想到最快的...一时脑浆糊了..谢谢了 ...

看来我还是要多看PHP手册,又被xu版大人秒杀了。

发现数组中一个奇怪的问题,不知道是不是我写的有问题.

$strArr = array(    1 => 0, 3 => 1, 6 => 2, 10 => 3, 15 => 4, 21 => 5, 28 => 6, 36 => 7, 45 => 8,    55 => 9, 63 => 10, 69 => 11, 73 => 12, 75 => 13, 75 => 14, 73 => 15, 69 => 16,    63 => 17, 55 => 18, 45 => 19, 36 => 20, 28 => 21, 21 => 22, 15 => 23, 10 => 24, 6 => 25,    3 => 26, 1 => 27);echo $str = '1,2,3,6,7,25,23,15,18'."\n";$sArr = explode(',',$str);foreach($sArr as $v){    echo array_search($v,$strArr)."\n";}


结果:
1,2,3,6,7,25,23,15,186157355Array(    [1] => 27    [3] => 26    [6] => 25    [10] => 24    [15] => 23    [21] => 22    [28] => 21    [36] => 20    [45] => 19    [55] => 18    [63] => 17    [69] => 16    [73] => 15    [75] => 14)


我的0-13的这个怎么都没有了???

不明白这个 0-13 是什么意思

你的 $strArr 数组键名重复了,因此后面的覆盖前面的。print_r($strArr); 就知道了。

$strArr = array(
1 => 0,
3 => 1,
6 => 2,
10 => 3,
15 => 4,
21 => 5,
28 => 6,
36 => 7,
45 => 8,
55 => 9,
63 => 10,
69 => 11,
73 => 12,
75 => 13,
75 => 14,
73 => 15,
69 => 16,
63 => 17,
55 => 18,
45 => 19,
36 => 20,
28 => 21,
21 => 22,
15 => 23,
10 => 24,
6 => 25,
3 => 26,
1 => 27
);
套红的下标将覆盖上面已有的下标