Calculate an algorithm! Solution

Source: Internet
Author: User
An algorithm !! Existing range: $ start -- $ end & nbsp; (for example, 5--10 ). Give a variable $ param: if $ param & lt; $ start & nbsp; then $ param = $ start if $ param & gt; $ end & nbsp; then $ param = $ end does not require if & nbsp; e to calculate an algorithm !!
The existing range is $ start -- $ end (for example, 5--10 ).
Give a variable $ param:
If $ param <$ start, $ param = $ start
If $ param> $ end, $ param = $ end
No logic algorithms such as if else and ternary (although simple and efficient)
To be realistic, share the following mathematical algorithms: $ end ??... 'Data-pics = ''>
------ Solution --------------------
Can it be done?
The answer is yes. Otherwise, I won't pin him up.

Let me start with a discussion.
function foo($a, $b, $c) {
  $f1 = -(($c - $a) >> 31);
  $f2 = -(($b - $c) >> 31);
  $f3 = 1 ^ ($f1 + $f2);
  return $f1 * max($a, $c) + $f2 * min($b, $c) + $f3 * $c;
}

echo foo(5, 10, 3); //5
echo foo(5, 10, 8); //8
echo foo(5, 10, 13); //10

------ Solution --------------------

function foo($a, $b, $c) {
Return (((($c-$a) + (($c-$a)  >> 31)) ^ (($c-$a) >> 31)) - ((($c-$b) + (($c-$b)  >> 31)) ^ (($c-$b) >> 31)) + $a + $b) / 2;
}

echo foo(20, 30, 50);  // 30

This does not use any function pulling.
What I got from the search,
Algorithms need to be studied.
------ Solution --------------------
Reference:
Can it be done?
The answer is yes. Otherwise, I won't pin him up.

Let me start with a discussion.
PHP code
Function foo ($ a, $ B, $ c ){
$ F1 =-($ c-$ a)> 31 );
$ F2 =-($ B-$ c)> 31 );
$ F3 = 1 ^ ($ f1 + $ f2 );
Return $ f1 * max ($ a, $ c) + $ f2 * min (......


I don't understand PHP... but the answer is too obvious.
So write a C version int ..
#include
 
  
int middle(int a,int b,int c)
{
    long long la=a;
    long long lb=b;
    long long lc=c;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.