Home > Article > Backend Development > 获得数组里的数字连号问题
$result = array(
'1,3,5,6,7,8,9,10,14,15,18,100,101',
'1,2,5,6,7,8,9,10,14,15,18,100,101,102'
);
求一个函数能计算数字元素包含连号的类型与次数
例如:第一组: 6连号1组,2连号2组
第一组: 6连号1组,2连号2组,3连号1组
我目前程序只能做到 1,2,5,6,7,8,9,10, 认为这是2连号6组!想了好久
放到数据库里,用自连接轻松搞定
楼上倒是给语句方案啊
放到数据库里,用自连接轻松搞定
mysql_connect();mysql_select_db('test');mysql_query('DROP TABLE IF EXISTS lookup');mysql_query('CREATE TABLE lookup (id INT, INDEX USING BTREE (id)) ENGINE = MEMORY');$a = array(1,3,5,6,7,8,9,10,14,15,18,100,101);foreach($a as $v) mysql_query("insert into lookup values ($v)");for($n=2; $n<5; $n++) { echo "<h5>$n</h5>"; $rs = mysql_query("select a.id, group_concat(b.id) as grouping, count(*) as cnt from lookup a, lookup b where a.id<=b.id and a.id>b.id-$n group by a.id HAVING cnt=$n") or die(mysql_error()); while($row = mysql_fetch_assoc($rs)) { echo $row['grouping'], '<br>'; }}
function LinkNum($array,$num){ if(count($array) < $num) { return 0; } $cishu = 0; //临时变量 $total = 0; //N连号次数 for($i=0;$i<count($array);$i++) { if($array[$i] == $array[$i+1]-1) { $cishu++; if($cishu == $num) { $i = $i+1; $cishu = 0; } } else { if($cishu == $num -1 && ($i - $num <= 0 || $array[$i] - $num != $array[$i - $num])) { $total++; } $cishu = 0; } } return $total;}echo "1,3,5,6,7,8,9,10,14,15,18,100,101".'有2连号'.LinkNum(array(1,3,5,6,7,8,9,10,14,15,18,100,101),2).'组'.'<br>';echo "1,3,5,6,7,8,9,10,14,15,18,100,101".'有3连号'.LinkNum(array(1,3,5,6,7,8,9,10,14,15,18,100,101),3).'组'.'<br>';echo "1,3,5,6,7,8,9,10,14,15,18,100,101".'有4连号'.LinkNum(array(1,3,5,6,7,8,9,10,14,15,18,100,101),4).'组'.'<br>';
1,3,5,6,7,8,9,10,14,15,18,100,101有2连号2组 ?
1,3, 5,6, 7,8, 9,10, 14,15,18, 100,101有2连号2组
不是 5 组吗?
还没有算 8,9
1,3,5,6,7,8,9,10,14,15,18,100,101有2连号2组 ?
1,3, 5,6, 7,8, 9,10, 14,15,18, 100,101有2连号2组
不是 5 组吗?
嗯,不计及连续数子集
$a = array(1,3,5,6,7,8,9,10,14,15,18,100,101);print_r(consecutive_numbers($a));echo consecutive_numbers($a, 4);function consecutive_numbers($ar,$num=0) { $res = array(); for($i=0; $i<count($ar)-1; $i++) { for($j=$i+1; isset($ar[$j]) && $ar[$j]-$ar[$i] == $j-$i; $j++); if($j-$i > 1) { $res[$j-$i][] = join(",", array_slice($ar, $i, $j-$i)); $i = --$j; } } if($num) return count(@$ret[$num]); return $res;}
Array( [6] => Array ( [0] => 5,6,7,8,9,10 ) [2] => Array ( [0] => 14,15 [1] => 100,101 ))0
嗯,不计及连续数子集
$a = array(1,3,5,6,7,8,9,10,14,15,18,100,101);print_r(consecutive_numbers($a));echo consecutive_numbers($a, 4);function consecutive_numbers($ar,$num=0) { $res = array(); for($i=0; $i<count($ar)-1; $i++) { for($j=$i+1; isset($ar[$j]) && $ar[$j]-$ar[$i] == $j-$i; $j++); if($j-$i > 1) { $res[$j-$i][] = join(",", array_slice($ar, $i, $j-$i)); $i = --$j; } } if($num) return count(@$ret[$num]); return $res;}
Array( [6] => Array ( [0] => 5,6,7,8,9,10 ) [2] => Array ( [0] => 14,15 [1] => 100,101 ))0
$ret 是数组,前面已经有定义($ret = array(); )
直接 $ret[$num] 可能引发 E_NOTICE 级别错误($ret[$num] 可能不存在,比如 $ret[4])
加个 @ 就可屏蔽掉错误信息
或者你改作 return count(isst($ret[$num] ? $ret[$num] : array());
$ret 是数组,前面已经有定义($ret = array(); )
直接 $ret[$num] 可能引发 E_NOTICE 级别错误($ret[$num] 可能不存在,比如 $ret[4])
加个 @ 就可屏蔽掉错误信息
或者你改作 return count(isst($ret[$num] ? $ret[$num] : array());
你那是什么 IDE?