//用从1到9九个数组成三个三位数,这三个三位数相加等于999,每个数字不可重复使用,用php代码算出是那几组三位数
//三个数字为$bai$shi $ge百位,十位, 个位
function get_999(){
//缩小范围,最小123,最大987
for($num1 = 123; $num1<=987; $num1++){
//找出第二个数字,和第三个数字
//
for($num2 = $num1+1; $num2<=987; $num2++){
$num3 = 999 - $num1- $num2;
//第三个数字要大于第二个数字
if($num3continue;
//判断所有的数字,是否重复,是否含有0
if(is_duplicate($num1, $num2, $num3))
continue;
printf("三位数字为 %d, %d, %d
", $num1, $num2, $num3);
}
}
}
//判断每位数是否重复, 是否含有0
function is_duplicate($num1, $num2, $num3){
/* $arr1 = array_flip(get_num($num1));
$arr2 = array_flip(get_num($num2));
$arr3 = array_flip(get_num($num3)); */
$arr1 = get_num($num1);
$arr2 = get_num($num2);
$arr3 = get_num($num3);
$result = array_merge($arr1, $arr2, $arr3);
$count = array_count_values($result);
//不能有0
if(array_key_exists(0, $count))
return true;
if(count($count) == 9)
return false;
else
return true;
}
//求出三位数的百位数,十位数,个位数
function get_num($num){
$data[] = intval(floor($num/100));
$data[] = intval(floor($num%100/10));
$data[] = intval(floor($num%10));
return $data;
}
get_999();