21,886
社区成员
发帖
与我相关
我的任务
分享
$interval = array(5, 10);
$x = 8;
$interval[] = $x;
sort($interval);
echo $interval[2];
#include <iostream>
using namespace std;
int func(int i, int start, int end)
{
bool bs = start / i;
bool be = i / end;
i -= ((i - end) * be);
i += ((start - i) * bs);
return i;
}
int main()
{
int i;
while (cin >> i)
{
cout << func(i, 5, 10) << endl;
}
}
/** 取“负值”。
* 如果 $a 为负数,则返回 $a,否则返回 0。
*/
function get_neg( $a )
{
return -1 * ( ( $a - 1 ) >> 63 ) * $a;
}
/** 取“大于等于值”。
* 如果 $a 大于等于 $lower_bound,则返回 $a,否则返回 $lower_bound。
*/
function get_ge( $a, $lower_bound )
{
return $a - get_neg( $a - $lower_bound );
}
/** 取“小于等于值”。
* 如果 $a 小于等于 $upper_bound,则返回 $a,否则返回 $upper_bound。
*/
function get_le( $a, $upper_bound )
{
return $a + get_neg( $upper_bound - $a );
}
/** 取“区间内的值”。
* 如果 $a 在区间 [ $lower_bound, $upper_bound ] 之内,则返回 $a,否则返回最接近的边界值。
*/
function get_inside( $x, $lower_bound, $upper_bound )
{
return get_le( get_ge( $x, $lower_bound ), $upper_bound );
}
$x = array( -3.7, -3, -2.8, -1.7, -1.2, -0.1, 0, 0.0001, 0.1, 1.2, 1.7, 2.8, 3, 3.7 );
$lower_bound = -1.7;
$upper_bound = 1.7;
foreach ( $x as $value ) {
$neg = get_neg( $value );
$inside = get_inside( $value, $lower_bound, $upper_bound );
printf( "%8s => %8s => %8s\r\n", $value, $neg, $inside );
}
// 输出结果如下:
// -3.7 => -3.7 => -1.7
// -3 => -3 => -1.7
// -2.8 => -2.8 => -1.7
// -1.7 => -1.7 => -1.7
// -1.2 => -1.2 => -1.2
// -0.1 => -0.1 => -0.1
// 0 => 0 => 0
// 0.0001 => 0 => 0.0001
// 0.1 => 0 => 0.1
// 1.2 => 0 => 1.2
// 1.7 => 0 => 1.7
// 2.8 => 0 => 1.7
// 3 => 0 => 1.7
// 3.7 => 0 => 1.7