欢迎访问 如意编程网!

如意编程网

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

2018 kick start roundA A Even_digits

发布时间:2024/5/15 编程问答 4 豆豆
如意编程网 收集整理的这篇文章主要介绍了 2018 kick start roundA A Even_digits 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目描述:

找出距离num最近的所有位都为偶数的数,首先分为三部分

第一部分:

所有数都是偶数,则返回0

第二部分:找big_num

如果存在奇数,则判断该奇数是不是9

  • 不是9,则big_num为该位add 1,后面剩余位为0
  • 为9,判断前一位是不是8,不是8,则前一位add 2,后面剩余位为0
  • 为9,且前一位为8,则距离最近的那个数就是small_num,即该位即以后所有位都为8

第三部分:找small_num

如果存在奇数,则该位sub 1,后面剩余位都为8

# -*- coding: utf-8 -*- """ Created on Tue Jul 7 15:24:35 2020@author: natur """def check_bit_com_big(num):num = str(num)big_num_test=""big_num = ""for i in range(len(num)):num_bit = num[i:i+1]num_bit = int(num_bit)big_num_test +=str(num_bit)if num_bit in odd_list:big_num += big_num_test[0:i]num_bit = num_bit + 1big_num += str(num_bit)big_num += str(0)*(len(num)-i-1)breakreturn(big_num)def special_9_check_bit_com_big(num):num = str(num)big_num_test=""big_num = ""for i in range(len(num)):num_bit = num[i:i+1]num_bit = int(num_bit)big_num_test +=str(num_bit)if num_bit == 9:#mett_89if int(num[i-1:i]) == 8:big_num += big_num_test[0:i]num_bit = 8big_num += str(num_bit)big_num += str(8)*(len(num)-i-1)breakelse:pre_bit = int(num[i-1:i]) + 2big_num += big_num_test[0:i-1]big_num += str(pre_bit)big_num += str(0)*(len(num)-i)breakelif num_bit in odd_list:big_num += big_num_test[0:i]num_bit = num_bit + 1big_num += str(num_bit)big_num += str(0)*(len(num)-i-1)breakelse:return "0"return(big_num)def check_bit_com_small(num):num = str(num)small_num_test=""small_num = ""for i in range(len(num)):num_bit = num[i:i+1]num_bit = int(num_bit)small_num_test +=str(num_bit)if num_bit in odd_list:small_num += small_num_test[0:i]num_bit = num_bit - 1small_num += str(num_bit)small_num += str(8)*(len(num)-i-1)breakelse:return "0"return(small_num)def begin_num(num):big_num = int(special_9_check_bit_com_big(num))small_num = int(check_bit_com_small(num))big_button = big_num-numsmall_button = num-small_numif big_num == 0:true_button = 0elif big_button<0:true_button = small_buttonelse:true_button = min(big_button,small_button)return true_buttonif __name__ == "__main__":even_list = [0,2,4,6,8]odd_list = [1,3,5,7,9]round = int(input())for i in range(round):num = int(input())true_button = begin_num(num)print("Case #"+str(i+1)+":"+" "+str(true_button))

test

总结

以上是如意编程网为你收集整理的2018 kick start roundA A Even_digits的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得如意编程网网站内容还不错,欢迎将如意编程网推荐给好友。