Good evening. Tell me please. The task is:

Using the ternary operation 1?2:3 and the necessary arithmetic operations, make a program that performs the following actions z = xy, если x>y и z= y-x+1, если x<=y .

I can only find the minimum. How to set operations? Waiting for tips, thanks in advance!

    5 answers 5

      z = x > y ? x - y : y - x + 1; 

    PS I do not understand where the problem arose.

    • The problem is that I slow down with algorithmization. on the move just bring the minimum of them. But not like that right away :) - Kollibry

    For example:

     z = ( x > y ) ? ( x - y ) : ( y - x + 1 ); 

    It is possible so:

     z = ( x - y ) * ( ( x > y ) ? 1 : -1 ) + ( ( x > y ) ? 0 : 1 ); 

      Once such a booze has gone (I mean the answer @GLmonster ), I will bring my 5 kopecks

       ((z = x - y) > 0) || (z = 1 - z); 
      • and the order of calculation of the operands around || defined? - andrybak
      • As far as I know, the left part is calculated first, if it is false, then the right part. In any case, everything works in gcc. - skegg
      • I still have to study and learn, I know the syntax, but I don’t know how to twist like that =) - sudo97
      • And do not twist, it's all so, empty profilometry. - avp
      • @avp, bebebe. Obviously, this is fun and I will not write like this in a real program. Maybe you offer your version? - skegg

      And how do you like this non-standard option?

       x>y && z=xy; x>y || z=y-x+1; 

      Of course, there is no ternary operation here, but it has already been brought. As a last resort, if necessary, can be done 1? 2: 3; as wrote @zhioev .

      After the first comment there were more ideas:

      First, it is possible without brackets:

       x>y && z=xy || z=y-x+1 

      If you can rewrite it like this:

       x<=y && z=y-x+1 || z=xy 

      Only these examples will work correctly, because if x> y, then z will never be equal to 0, and there will be no problems with the fact that the second condition starts to be fulfilled, if the first is true. In other cases, you can easily make a mistake.

      • 2
        Then too: ((x> y) && (z = x - y)) || (z = y - x + 1) - timka_s
      • one
        It is better not to write without brackets ... Suddenly, in some language, the Priority (||) is greater than the Priority (&&) ... Yes, and just remembering priorities one more time is not gud ... - timka_s
      • you and straight monster. rich is not Russian, but C ++ =) - sudo97
      • > Without brackets, it is better not to write ... Suddenly, in some language, the Priority (||) is greater than the Priority (&&) ... According to the rules of computer science, logical AND priority is higher than OR. Logical AND is also called logical multiplication, and OR - logical addition. Even according to the rules of mathematics it turns out the same. I do not think that some language will trample against these rules. Moreover, we are talking about C \ C ++. > Yes, and once again remember the priorities-is not gud ... Anyway, to use such a non-standard version is not gud. Compared to its use, priorities are a trifle. - devoln
       int x, y, z; ... if (x > y) { z = x - y; } else { z = y - x + 1; } 1 ? 2 : 3; 
      • it is said after all - with ternary operations. - sudo97
      • And I have without ternary? - dzhioev