求一算法!!
现有一区间:$start--$end (例:5--10)。
给一变量$param:
如果$param<$start 则$param=$start
如果$param>$end 则$param=$end
不用if else,三元 等逻辑算法(虽然简单并且效率也高)
求实现此要求的数学算法
算法有待研究。
[解决办法]
不懂PHP...但是这个答案提示也太明显了
于是写个C版本int的..
#include<stdio.h>
int middle(int a,int b,int c)
{
long long la=a;
long long lb=b;
long long lc=c;
long long f1=(lc-la)>>63&0x1;
long long f2=(lb-lc)>>63&0x1;
long long f3=1^(f1+f2);
return f1*a+f2*b+f3*c;
}
int main()
{
int a,b,c;
while(~scanf("%d%d%d",&a,&b,&c))
{
printf("%d\n",middle(a,b,c));
}
}
转为longlong 是为了防止溢出
测试如下:
5 10 3
5
5 10 8
8
5 10 13
10
-2000000000 2000000000 -1000000000
-1000000000
-2000000000 2000000000 1000000000
1000000000
-2000000000 2000000000 -2000000001
-2000000000
-2000000000 2000000000 2000000001
2000000000
只是感觉这样效率很低...
[解决办法]
public static int get(int start, int end, int param)
{
param -= start;
param &= (int)(((uint)param) >> 31) - 1;
param += start;
param -= end;
param &= (int)((((uint)param) >> 31) ^ 1) - 1;
return param += end;
}
public static int get1(int start, int end, int param)
{
return new int[] { end, param, start, start }[(((uint)(param - start) >> 30) & 2)
[解决办法]
((uint)(param - end) >> 31)];
}
用符号位只能算正数,否则要将int扩展到long
[解决办法]
不知道这个是不是楼主想要的。
int foo(int start, int end, int param)
{
int tmp[2];
tmp[0] = param;
tmp[1] = start;
param = tmp[(param-start)>>(sizeof(int)*8-1)];
tmp[1] = end;
param = tmp[(end-param)>>(sizeof(int)*8-1)];
return param;
}
[解决办法]
<?php
//过滤掉全部的负数,即:当x>0时返回x,否则返回0
function NegativeNumber($x){
return (abs($x) + $x) / 2;
}
//过滤掉全部的正数,即:当x<0时返回x,否则返回0
function PositiveNumber($x){
return ($x - abs($x)) / 2;
}
//选两个数中最大的一个,即当a>b时返回a,否则返回b
function MyMax($a, $b){
return (NegativeNumber($a - $b) + NegativeNumber($b - $a) + $a + $b) / 2;
}
function MyMin($a, $b){
return (PositiveNumber($a - $b) + PositiveNumber($b - $a) + $a + $b) / 2;
}
function MyRange($x, $min, $max){
return MyMin(MyMax($x, $min), $max);
}
$start = 5;
$end = 10;
for($i = 0; $i < 20; $i++){
echo($i);
echo('=');
echo(MyRange($i, $start, $end));
echo("<br>");
}
?>
[解决办法]
你们这些人....太有闲工夫了....有这时间,M几个L不好吗?
好吧,今天...我也有点闲.....
用函数的话,方法很多....所以不用函数...
下面这个, 主体都是在cal这个函数里, 其它部分是做测试用
.....除了加减乘除,还用到了!和(int), 不过(int)不算函数哦,亲!
嗯,30楼的很棒,不过有两个限制,一个是必须是整数,二是必须是32位机.....我这个没有这两个限制哦,亲!
随便说一句,淘宝体很好,建议在php论坛推广,大家要支持哦,亲!
<?php
function cal($small,$big,$x){
$t1=$small * (int)(($small+$small*$small+$x*$x+1)/($x+$small*$small+$x*$x+1)); // $x<=$small
$t2=$x * (int)(($x+$small*$small+$x*$x+1)/($small+$small*$small+$x*$x+1)); // $x>=$small
$t3=$x * (int)(($big+$big*$big+$x*$x+1)/($x+$big*$big+$x*$x+1)); // $x<=$big
$t4=$big * (int)(($x+$big*$big+$x*$x+1)/($big+$big*$big+$x*$x+1)); // $x>=$big
$t5=2*($t2*$t3)/($t2+$t3);
return $t1+$t4+$t5*(!(int)($t1+$t4));
}
/////////////////////////////////////////////////////////////////////////
function check($small,$big,$x){
return min(max($x,$small),$big);
}
function test($small,$big,$x){
$correct = check($small,$big,$x);
$display = false;
$result = cal($small,$big,$x);
if(abs($result-$correct)>0.000000001){
$display = true;
}
if($display){
echo $small,'
[解决办法]
',$big,'
[解决办法]
',$x,' ==> ',$correct,' = ',$result;
echo "\n\n";
}
}
test(-20,-10,-28);
test(-20,-10,-17);
test(-20,-10,-1);
test(-20,-10,11);
test(-10,3,-200);
test(-10,3,-2);
test(-10,3,1);
test(-10,3,285);
test(5,10,-21);
test(5,10,3);
test(5,10,5);
test(5,10,8);
test(5,10,12);
test(5,10,84);
test(3.2,6.7,1.0);
test(3.2,6.7,3.2);
test(3.2,6.7,3.8);
test(3.2,6.7,9);
test(-11.8,-5.3,-12);
[解决办法]
好,继续闲.....
版本2, 没有再用(int)和!哦, 亲!
不过用到了数组的一个特别特性...我的php版本是5.3.2,欢迎在不同的版本上测试哦,亲!
function cal($small,$big,$x){
$b[0]=1;
$b[($x+$small*$small+$x*$x+1)/($small+$small*$small+$x*$x+1)]=0;
$tmp = $small * (1-$b[0]) + $x * $b[0] ;
$b[0]=1;
$b[($tmp+$big*$big+$tmp*$tmp+1)/($big+$big*$big+$tmp*$tmp+1)]=0;
return $big * $b[0] + $tmp * (1-$b[0]) ;
}