求一算法!!
现有一区间:$start--$end (例:5--10)。
给一变量$param:
如果$param<$start 则$param=$start
如果$param>$end 则$param=$end
不用if else,三元 等逻辑算法(虽然简单并且效率也高)
求实现此要求的数学算法
算法有待研究。
[解决办法]
#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));
}
}
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 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>");
}
?>
<?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);
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]) ;
}