Find an algorithm!!

Source: Internet
Author: User
Tags integer division
Existing range: $start-$end (example: 5--10).
Give a variable $param:
If $param< $start then $param= $start
If $param> $end then $param= $end
Do not use if else, ternary logic algorithms (albeit simple and high efficiency)
A mathematical algorithm for achieving this requirement


Reply to discussion (solution)

I can't.

This algorithm does not exist (no comparisons are used), and in the case of explanatory languages, do not think that bit arithmetic will increase speed.

Even if it exists, it must contain 2 multiplication and one division, which is time-consuming than if-else.

So, there is no.

Example: 5--10 (x-y)
A=x-z
B=y-z

Assuming that the number of judgements is 1 (z)
Because a is greater than 0,b greater than 0. So z=x=5.


Assuming that the number of judgements is one (z)
Because a is less than 0,b less than 0. So z=y=10.


Assuming that the number of judgements is 6 (z)
Because a is less than 0,b greater than 0. So not set up.


The egg hurts.

Tried or rounded with a different mode ... All kinds of combinations, really don't get the answer
We discuss the possibility of implementing this algorithm

Landlord seeking algorithm Ah, this really no =_= ...

This algorithm does not exist (no comparisons are used), and in the case of explanatory languages, do not think that bit arithmetic will increase speed.

Even if it exists, it must contain 2 multiplication and one division, which is time-consuming than if-else.

So, there is no.

The formula itself is estimated to exist. Do not use judgment
However, there really is not a very fast formula, I deduced a moment ago, found that the multiplication/division operation must be used.

Why is my 4 floor swallowed?

I tried. Multiple combinations of operations never get results
Let's discuss the possibility of realization.

The derivation process
The formula itself is estimated to exist. Do not use judgment
However, there really is not a very fast formula, I deduced a moment ago, found that the multiplication/division operation must be used.

x = 5 (y <= 5)
x = y (y in [5,10] interval)
x = ten (y >= 10)
------------------------------------------------------------
The landlord draws a coordinate chart, basically equivalent to a lightning pattern of a curve (), when learning geometry has not seen this curve equation.
|
|
|
/(10)
/
/
| (5)
|
|
|
|

Does not use the comparison really can realize?? I doubt it.

A, b E [5,10]
x=a+1;
X=b-1;
and a
A lot of value ....

Passing PHP should be able to do!



Mathematics is not good, temporarily unexpectedly, in no interval (that is, without if) conditions, this f (x) solution ... Wait for the math emperor ...

Can use Min,max is not very simple?

$param = 4;echo min (max ($param, 5), 10);

It's all right.

Do. Have min have max ...



Guys, go on.

And I learned that.

But consider that Max Min's mechanism should be implemented internally using logical operations.

I woke up in a dream.
function Getnum ($param, $start, $end)
{
$min = min ($param, $start, $end);
$max = Max ($param, $start, $end);

if ($min = = $param)
{
$param = $min;
}

if ($max = = $param)
{
$param = $max;
}

return $param;
}

Can use Min,max is not very simple?
PHP Code
$param = 4;
Echo min (Max ($param, 5), 10);

It's all right.

=_=...
Foolbirdflyfirst's got a code. You also write

I woke up in a dream.
function Getnum ($param, $start, $end)
{
$min = min ($param, $start, $end);
$max = Max ($param, $start, $end);

if ($min = = $param)
{
$param = $min;
}

if ($max = = $param)
{
$param ...

At least respect the fruits of other people's work.


=_=...
Foolbirdflyfirst's got a code. You also write

Reference 21 Floor Jasmine_xiaocao's reply:

I woke up in a dream.
function Getnum ($param, $start, $end)
{
$min = min ($param, $start, $end);
$max = Max ($param, $start, $end);

if ($mi ...

This classmate, you digress seriously ah hehe ~ ~
At least respect the fruits of other people's work.

Reference 22 Floor ci1699 's reply:

=_=...
Foolbirdflyfirst's got a code. You also write

Reference 21 Floor Jasmine_xiaocao's reply:

I woke up in a dream.
function Getnum ($param, $start, $end)
{
$min = min ($param, $start, $end);
......


What does the landlord mean???
This classmate, you digress seriously ah hehe ~ ~
Reference 23 Floor Jasmine_xiaocao's reply:
At least respect the fruits of other people's work.

Reference 22 Floor ci1699 's reply:

=_=...
Foolbirdflyfirst's got a code. You also write

Reference 21 Floor Jasmine_xiaocao's reply:

I woke up in a dream.
function Getnum ($param, $start,......

The topic said cannot use if else's Oh Pro ~ landlord what meaning???
Reference 24 Floor Mr_merlin's reply:

This classmate, you digress seriously ah hehe ~ ~
Reference 23 Floor Jasmine_xiaocao's reply:
At least respect the fruits of other people's work.

Reference 22 Floor ci1699 's reply:

=_=...
Foolbirdflyfirst's got a code. You also write

Reference 21 Floor Jasmine_xiaocao's reply:

A language awakened dream ...

That's not going to work?? Then I was wrong ....

The title said can not use if else's Oh Pro ~ Quote 25 floor Jasmine_xiaocao reply:
What does the landlord mean???
Reference 24 Floor Mr_merlin's reply:

This classmate, you digress seriously ah hehe ~ ~
Reference 23 Floor Jasmine_xiaocao's reply:
At least respect the fruits of other people's work.

Reference 22 Floor ci1699 's reply:

=_=...
Foolbirdflyfir ...

Can use Min,max is not very simple?
PHP Code
$param = 4;
Echo min (Max ($param, 5), 10);

It's all right.
Good, if can not use Min,max?

I haven't thought about it for a while.
Looks like my algorithm is poor.

Learning and Learning

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

This does not use any function to pull.
I went to seek the scripture,
The algorithm needs to be researched.

With ABS, it's easy to understand.

Reference 17 Floor Foolbirdflyfirst's reply:

Can use Min,max is not very simple?
PHP Code
$param = 4;
Echo min (Max ($param, 5), 10);

It's all right.

Good, if can not use Min,max?
I don't know... #30的人才啊. Ha ha

I don't know

The numbers are converted into nether and upper bounds, respectively.
The results obtained are

Example: (5,10,x) x=8
X1 = (MOD5)
x2 = (MOD10)
Answer = (X1MSB⊙X2MSB) * (x1msb*5+x2msb*10) + (X1MSB xor X2MSB) *x

The X1MSB X2MSB here needs to be processed by the high-order integer division to get the binary 0,1

Landlord Mighty, do not know what you are thinking

Wait and see ....

Pass the study.

Is there an algorithm for this implementation?

x = 5 (y <= 5)
x = y (y in [5,10] interval)
x = ten (y >= 10)
------------------------------------------------------------
The landlord draws a coordinate chart, basically equivalent to a lightning pattern of a curve (), when learning geometry has not seen this curve equation.
|
|
|
/(10)
/
/
| (5)
|
|
|
|

As the 9 floor said, if the main building is a mathematical algorithm, this
?? ?? ?? ?? ?? ?? ??
/
/
/
/
/
?? ?? ?? ?? ?? ?? ??
A similar function curve has never been seen.
Can only be a piecewise function, divided into three paragraphs, so it seems to have to use if else.

Well--How did my diagram become like this ....
That curve is a z-like Y axisymmetric shape, so it should be just a piecewise function? Because it's not smooth, there's a corner.
Reference 9 Floor Foolbirdflyfirst's reply:

x = 5 (y <= 5)
x = y (y in [5,10] interval)
x = ten (y >= 10)
------------------------------------------------------------
The landlord draws a coordinate chart, basically equivalent to a lightning pattern of a curve (), when learning geometry has not seen this curve equation.
|
|
......

public static int get (int start, int end, int param)        {            param-= start;            param &= (int) ((UINT) param) >>-1;            param + = start;            param-= end;            param &= (int) (((UINT) param) >> ^ 1)-1;            return param + = end;        }        public static int Get1 (int start, int end, int param)        {            return new int[] {end, param, start, start}[((UINT) (PA Ram-start) >> & 2) | (UINT) (param-end) >>)];        }

Use the sign bit to count only positive, or expand int to long

I don't know if this is what the landlord wants.

int foo (int start, int end, int param) {int tmp[2];tmp[0] = param;tmp[1] = Start;param = tmp[(param-start) >> (sizeof (i NT) *8-1)];tmp[1] = End;param = tmp[(end-param) >> (sizeof (int) *8-1)];return param;}

param = ((start+end)-abs (start-end))/2, where ABS () is an absolute value operation.

param = (ABS (Param-start)-abs (param-end) +start+end)/2;

I really can't read it.

I don't know.

  • 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.