2010-01-31 50 views
0

我有一个复杂的算法,我尝试在PHP中进行编码,但对于像我这样的初学者来说,这有点复杂。所以我需要你的帮助
我有一个数组未知的数字从t1开始到tn。我不知道它的长度actually.like
如何在PHP中对此算法进行编码?

(t1,t2,t3,t4,.....,tn) 

而且我想在第一elemnt如果没有测试条件,最后一个元素上如果不将测试对价值的共同条件从T2到TN -1
我也有值,称为s,c & b,我将在代码中使用。
第一个条件是:

if c < s -> Do something 
if c >= s and c < (s + (t1 - t2)) -> Do something else 
if c >= (s + (t1 - tn)) and c < (s + (t1 + b)) -> Do something else 

如果上述条件不相匹配,然后我想以下列方式

if c >= (s + (t1 - t2)) and c < (s + (t1 - t3)) -> Do something that's bound to t2 value 
if c >= (s + (t1 - t3)) and c < (s + (t1 - t4)) -> Do somehting else 

等测试来自T2了值TN-1上至tn-1 ..我不知道阵列中有多少值,所以我需要动态执行此操作

任何人都可以帮助我吗?这将是一个很大的帮助确实

回答

1

让我们想象一下,这不是你的家庭作业。

第一个条件是枯燥的,它的IF语句以检查,以确保t2存在和count()找到tn。第二部分稍微有趣:

$homework = array(t1,t2....tn); 

for (
    $i=2,$n=count($homework),$cs=$c-$s,$cnd_a=$homework[0]-$homework[1];   
    $i<$n;++$i) 
{ 
    $cnd_b = $homework[0] - $homework[$i]; 
    if ($cs >= $cnd_a && $cs < $cnd_b) do_no_study($homework[$i-1]); 
    $cnd_a = $cnd_b; 
} 

或者沿着这些线。

0

这可能是为你揭开序幕:

<?php 

function check_condition($value, $pos) 
{ 
    // do your magic 
    return TRUE; // or FALSE; 
} 

$values = array(1,2,3,4,5,6,7,8,9); 

// Array length 
$arr_length = length($values); 

// Check the first condition 
for ($i=0; $i<($arr_length/2); $i++) 
{ 
    if (check_condition($values[$i], $i)) 
    { 
     echo "condition met at start"; 
     break; 
    } 
    if (check_condition($values[$i], $arr_length-$i)) 
    { 
     echo "condition met at start"; 
     break; 
    } 
} 

?> 
0

试试这个:

$t = array(); // your array 
$len = count($t); 

if ($c < $s) 
    Do_something(); 
else if ($c >= $s && $c < ($s + ($t[0] - $t[1]))) 
    Do_something_else(); 
else if ($c >= ($s + ($t[0] - $t[$len - 1])) && $c < ($s + ($t[0] + b))) 
    Do_something_else_again(); 
else 
    for ($i = 1; $i < $len - 1; $i++) 
     if ($c >= ($s + ($[0] - $t[$i])) && $c < ($s + ($t[0] - $t[$i + 1))) 
      Do_something_thats_bound_to_value($t, $i); 

注意, “$ C> = $ s” 不是在第二个“if”声明中需要,除了清晰。

相关问题