dongx.duan's blog

By dongx.duan, 9 years ago, In English

535B - Tavas and SaDDas

import sys

mask = [(10**x, 2**x) for x in range(8, 0, -1)] 

def first_digit(x):
    while x > 10:
        x = x / 10
    return x

def get_mask(n):
    for m in mask:
        if m[0] < n:
            return m
    return None

def index(n):
    if n == 4:
        return 1
    if n == 7:
        return 2

    m = get_mask(n)

    if first_digit(n) == 4:
        return m[1] + index(n % m[0])
    else:
        return 2 * m[1] + index(n % m[0])

n = int( sys.stdin.readline() )
print( index(n) )

For test case 474744: python2.7 give what I want: 83; but python3.4 give 125. WHY?

Is there any special settings for python3.4?

  • Vote: I like it
  • +5
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +25 Vote: I do not like it

Python2 division is integer, unlike Python3's.

Change x / 10 to x // 10 and get AC.