趣味编程PHP

 
 
 
 1.斐波那契数
<?php 
// $arr=array[1,2,3,5,8,13,21,34,55]
sum();
function sum(){
	$arr[0]=1;
	$arr[1]=2;
	$sum=$arr[0]+$arr[1];
	for($i=2;$i<10;$i++){	
		$arr[$i]=$arr[$i-1]+$arr[$i-2];				
		$sum+=$arr[$i];		
	}
	echo $sum;
}
?>


2.1000以内的罗马数字的转换

<?php 
rom(863);
function rom($n){
$arr=array(
		0=>array("","I","II","III","IV","V","VI","VII","VIII","IX"),//个位
		1=>array("","X","XX","XXX","XL","L","LX","LXXX","XCC"),//十位
		2=>array("","C","CC","CCC","CD","D","DC","DCC","DCCC","CM")//百位
);


//echo $arr[0.5][1.5];exit;
for($m=0,$i=1000;$m<3;$m++,$i/=10){
	$t=intval(($n%$i)/($i/10));	
	echo $arr[2-$m][$t];
}
}

3.字符串翻转

<?php 
$str="we see you";
$res=reverse($str);
echo $res;
function reverse($var)
{
    $res="";
    for($i=0,$j=strlen($var);$i<$j;$i++)
     {
        $res=$var[$i].$res;
        
     }
    return $res;
}
?>


更简单的方法:php的strrev($string)函数,直接就翻转函数。




你可能感兴趣的:(趣味编程PHP)