Title: Given two 32-bit integers a and B, returns the larger of A and B. Requirements: no comparative judgment.
Do not write the principle, directly on the derivation chart:
Code:
#find out the larger number of two numbers without any comparative judgment.ImportRandomdefFlip (x):returnX^1#method to be testeddefbincompare (x, y): Z= X-y sigx= Flip ((x>>31) &1) Sigy= Flip ((y>>31) &1) Sigz= Flip (((z>>31) &1)) Sigxysam= Flip (sigx^Sigy) Sigxydiff= sigx^Sigy func= Sigxysam*sigz + sigxydiff*SIGXreturnFunc*x + Flip (func) *y#常规Correct methoddefNormalmethod (x, y):returnMax (x, y)#Logarithmic devicedefCompare (x, y):returnTrueifx = = yElseFalsencount=0maxValue= 1000000#1 million plays whileNcount <=maxvalue:x= Random.randint (-1000000, 1000000)#random number between plus and minus 1 milliony = random.randint (-1000000, 1000000) Binfunresult=bincompare (x, y) Normalfunresult=Normalmethod (x, y)if notCompare (Binfunresult, Normalfunresult):Print("fucked.") Print("Sample: ({},{})". Format (x, y)) Breakncount+ = 1Else: Print("Success.")
Python Programming Algorithm Practice _003