Блог пользователя one0one

Автор one0one, история, 5 лет назад, По-английски

http://codeforces.net/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://codeforces.net/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?

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +11 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
»
5 лет назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

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.