var a,b : integer; begin ReadLn(a,b); if a = b then WriteLn('='); if a >= b then WriteLn('>') else WriteLn('<'); end. |
2 answers
Also, for the sake of fun - you can do without nested if / else: :))
uses Math; var M : array [-1..1] of Char = '<=>'; a, b : integer; begin ReadLn(a, b); WriteLn(a,' ',M[Sign(ab)],' ',b); ReadLn end. Or even like this:
uses Math; var a, b : integer; M : array [-1..1] of String = ('less than', 'equal to', 'greater than'); begin ReadLn(a, b); WriteLn(a,' ',M[Sign(ab)],' ',b); ReadLn end. - @Barklay, does delphi catch the whole overflow? But in fact (for 32-bit int) a: -2000000000 b: 2000000000 a - b: 294967296 (yes, sign + !!!). - However, it is a long-known fact that
a - bcan be used to find out the sign, only for a limited range of data. - avp - Well, if you used such large numbers, then probably the best (i.e. short) solution would be a change in a program such as a number: a, b: int64; Sign () has an overloaded method that works with this range, so changing just one identifier in the program would solve the problem, however the same could be achieved without changing the type of numbers {a, b}, leaving it the same but then just make a type conversion in a call to the f-tion: M [Sign (int64 (a) -b)], which would automatically lead to a call to an overloaded function that works with int64. So the problem would be solved! - Barklay
- uses Math; var M: array [-1..1] of String = ('less than', 'equal to', 'greater than'); a, b: integer; // int64; begin a: = -2000000000; b: = 2,000,000,000; WriteLn (a, '', M [Sign (int64 (a) -b)], '', b); ReadLn end. - Barklay
- one@Barklay, that's right. The main thing in principle is not to forget about the problem of overflow. - avp
- @avp - I agree! Yes, and as for whether Delphi catches overflow or not, the answer is yes, of course it catches! With such an overflow, I have already given two different solutions above. - Barklay
|
Thanks, I figured it out myself:
var a,b: integer; begin ReadLn(a,b); if a = b then WriteLn('=') else if a >= b then WriteLn('>') else WriteLn('<'); end. - one@Joks, off topic, but read how to format the code correctly: vk.cc/3tlkey , and secondly, you should not ask questions so rashly, but sit and think what your program does. The program performs only what is required of it. Successes! begin ReadLn (a, b); if a = b then writeLn ('=') else if a> = b then writeLn ('>') else writeLn ('<'); end. - BogolyubskiyAlexey
- 3> = there is no use at all, = by that moment it has already been eliminated - Isaev
|