Fenghe's Blog

Even Digits – Google Kick Start Round A, 2018

Following is my first submission, which did not pass.

T = int(input())

for t in range(T):
    isPlus = 1
    str_num = input()
    
    i = 0
    for i in range(len(str_num)):
        if i == len(str_num) - 1:
            if int(str_num[i]) % 2:
                isPlus = -1
            else:
                isPlus = -2
            break
        curr_digit = int(str_num[i]) 
        if curr_digit % 2:
            next_digit = int(str_num[i + 1])
            if next_digit < 5 or curr_digit == 9:
                # minus
                isPlus = 0
            break
            
    res = 0
    digits_left =  len(str_num) - i - 1
    current = int(str_num[i:])
    if isPlus == 1:
        target = int(str(int(str_num[i]) + 1) + '0' * digits_left)
        res = target - current
    elif isPlus == 0: # minus
        target = int(str(int(str_num[i]) - 1) + '8' * digits_left)
        res = current - target
    elif isPlus == -1: # even originally
        res = 1
    else:
        res = 0
        
    print('Case #{}: {}'.format(t + 1, res))
        

I find the hint to be more complicated than mine, but it provides me a more systematic way to solve this problem.

I still can’t find the reason. Maybe think about it later or will you give me the answer?

Leave a Reply

Your email address will not be published. Required fields are marked *