数组 的一道题,有大神会的吗?
这是老师布置的作业,可是我不会,希望有大神帮我讲解,谢谢你
这是老师布置的作业,可是我不会,希望有大神帮我讲解,谢谢你
2020-03-23
$green=$blue=$red=[]; for($i=2;$i<=100;$i+=2){ $i%2==0&&$arr[$i]=$i; if($i%4==0&&$i%6!=0){ array_push($green,$arr[$i]); echo '<span style="color: green">'.$i.'</span>'.'#'; } else if($i%6==0&&$i%4!=0){ array_push($blue,$arr[$i]); echo '<span style="color: blue">'.$i.'</span>'.'#'; } else if($i%6==0&&$i%4==0){ array_push($red,$arr[$i]); echo '<span style="color: red">'.$i.'</span>'.'#'; } else{ echo '<span style="color: black">'.$i.'</span>'.'#'; } if($i%20==0) { echo '<br>'; } } print_r($green); echo '<br>'; print_r($blue); echo '<br>'; print_r($red);
<?php
// 初始化定义1-100的数组
$arr1 = array();
// 用for循环赋值
for($i = 0; $i < 100; $i++){
$arr1[$i] = $i+1;
};
// 遍历$arr1数组内容
$j = 0;
do{
if(is_int($arr1[$j]/4) == true && is_int($arr1[$j]/6) == true){
if(is_int($arr1[$j]/4) == true || is_int($arr1[$j]/6) == true){
echo "<span style='color:red';'>".$arr1[$j]."#</span><br />";
};
}else if(is_int($arr1[$j]/4) == true){
echo "<span style='color:yellow';'>".$arr1[$j]."#</span>";
}else if(is_int($arr1[$j]/6) == true){
echo "<span style='color:blue';'>".$arr1[$j]."#</span>";
}else if(is_int($arr1[$j]/2) == true){
echo "<span style='color:black';'>".$arr1[$j]."#</span>";
};
$j++;
}while($j < count($arr1));
?>
<?php // 初始化定义1-100的数组 $arr1 = array(); // 用for循环赋值 for($i = 0; $i < 100; $i++){ $arr1[$i] = $i+1; }; // 遍历$arr1数组内容 $j = 0; do{ if(is_int($arr1[$j]/4) == true && is_int($arr1[$j]/6) == true){ if(is_int($arr1[$j]/4) == true || is_int($arr1[$j]/6) == true){ echo "<span style='color:red';'>".$arr1[$j]."#</span><br />"; }; }else if(is_int($arr1[$j]/4) == true){ echo "<span style='color:yellow';'>".$arr1[$j]."#</span>"; }else if(is_int($arr1[$j]/6) == true){ echo "<span style='color:blue';'>".$arr1[$j]."#</span>"; }else if(is_int($arr1[$j]/2) == true){ echo "<span style='color:black';'>".$arr1[$j]."#</span>"; }; $j++; }while($j < count($arr1));?>
$str = ''; $green = $blue = $red = []; for($i=1,$j=1;$i<=100;$i++){ $remainder2 = $i % 2; if( $remainder2 == 0 ){ //取偶数 $remainder4 = $i % 4; $remainder6 = $i % 6; if( $remainder4 == 0 && $remainder6 != 0){ $green[] = $i; $str .= '<font color="green">'.$i.'</font>'; }else if($remainder4 != 0 && $remainder6 == 0){ $blue[] = $i; $str .= '<font color="blue">'.$i.'</font>'; }else if($remainder4 == 0 && $remainder6 == 0){ $red[] = $i; $str .= '<font color="red">'.$i.'</font>'; }else{ $str .= $i; } $str .= $j<10 ? '#' : '<br/>'; $j = $j == 10 ? 1 : $j+1; } } echo $str.'<br/>'; echo '是4的倍数而不是6的倍数:</br>'; print_r($green); echo '</br>是6的倍数而不是4的倍数:</br>'; print_r($blue); echo '</br>即是4的倍数又是6的倍数:</br>'; print_r($red);
$str = ''; $green = $blue = $red = []; for($i=1,$j=1;$i<=100;$i++){ $remainder2 = $i % 2; if( $remainder2 == 0 ){ $remainder4 = $i % 4; $remainder6 = $i % 6; if( $remainder4 == 0 && $remainder6 != 0){ $green[] = $i; $str .= '<font color="green">'.$i.'</font>'; }else if($remainder4 != 0 && $remainder6 == 0){ $blue[] = $i; $str .= '<font color="blue">'.$i.'</font>'; }else if($remainder4 == 0 && $remainder6 == 0){ $red[] = $i; $str .= '<font color="red">'.$i.'</font>'; }else{ $str .= $i; } $str .= $j<10 ? '#' : '<br/>'; $j = $j == 10 ? 1 : $j+1; } } echo $str.'<br/>'; echo '是4的倍数而不是6的倍数:</br>'; print_r($green); echo '</br>是6的倍数而不是4的倍数:</br>'; print_r($blue); echo '</br>即是4的倍数又是6的倍数:</br>'; print_r($green);
<?php
$count = 0;
$arr1 = array();
$arr2 = array();
$arr3 = array();
// 输出符合条件的数字
for($i=1;$i<=100;$i++){
if($i%4==0 && $i%6!==0){
$count++;
array_push($arr1,$i);
echo "<font color=green>$i</font>","#";
continue;
}else if($i%4!=0 && $i%6==0){
$count++;
array_push($arr2,$i);
echo "<font color=blue>$i</font>","#";
continue;
}else if($i%4==0 && $i%6==0){
$count++;
array_push($arr3,$i);
echo "<font color=red>$i</font>","#";
continue;
}else if($i%2==0){
$count++;
echo "<font color=black>$i</font>","#";
continue;
}
// 输出10个换行
if($count%10==0){
echo "<br/>";
}
}
echo "<br/>";
echo "是4的倍数而不是6的倍数:","<br/>";
echo "Array(";
for($i=0; $i<sizeof($arr1); $i++){
echo "[",$i,"]","=>",$arr1[$i]," ";
}
echo ")";
echo "<br/>";
echo "是6的倍数而不是4的倍数:","<br/>";
echo "Array(";
for($i=0; $i<sizeof($arr2); $i++){
echo "[",$i,"]","=>",$arr2[$i]," ";
}
echo ")";
echo "<br/>";
echo "既是4的倍数又是6的倍数:","<br/>";
echo "Array(";
for($i=0; $i<sizeof($arr3); $i++){
echo "[",$i,"]","=>",$arr3[$i]," ";
}
echo ")";
?>
举报