one0one's blog

By one0one, history, 5 years ago, In English

http://mirror.codeforces.com/contest/1196/submission/60503250 Got wrong answer

q = int(input())
 
for i in range(q):
	piles = [int(i) for i in input().split()]
	piles.sort()
	print(piles[1] + int((piles[2] - (piles[1] - piles[0])) / 2))

http://mirror.codeforces.com/contest/1196/submission/60503474 Got accepted

q = int(input())
 
for i in range(q):
	piles = [int(i) for i in input().split()]
	piles.sort()
	print(piles[1] + ((piles[2] - (piles[1] - piles[0])) // 2))

In my opinion, they do the same work but results are not the same. There is a difference between // operator and int() probably. I searched it but could not find anything. Does anyone know it?

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
5 years ago, # |
  Vote: I like it +11 Vote: I do not like it

when you use "/2" it becomes a float type and your wrong answer is because of the floating representation of numbers in python

>>> x=11529967141909565
>>> y=2*x+1
>>> y//2
11529967141909565
>>> int(y/2)
11529967141909566
>>> y/2
1.1529967141909566e+16
>>> 
»
5 years ago, # |
  Vote: I like it +13 Vote: I do not like it

As many have already pointed out, in Python 3 a/b is the floating point division, and a//b is the floored division. However note that in Python 2 both a/b and a//b are floored divisions when a and b are integers. To make Python 2 behave like Python 3, start the source code with from __future__ import division . I usually just stick to using // in both Python 2 and 3 when I want floored division.