下面是百度的编程笔试题:输入a,b,n三个整数,a、b的取值范围是在0-9之间,n的取值范围是在1-10**9,让求得(a**n)*b的值的数字的和。
""" 思路: 1、让用户输入a,b,n 2、判断a,b是否为整数,并且是否在0-9之间,判断n是否为整数,并且是否在1-10**9 3、计算(a**n)*b的值 4、对求得值进行切分并相加 """ """ # 1、让用户输入a,b,n while True: int_a = 0 int_b = 0 int_n = 0 data_sum = 0 try: int_a = int(input("请输入(0-9)之间的整数a:")) int_b = int(input("请输入(0-9)之间的整数b:")) int_n = int(input("请输入(1-10的9次方)之间的整数n:")) except: print("您输入的数值中包含非整数值!!!") continue # 2、判断a,b是否为整数,并且是否在0-9之间,判断n是否为整数,并且是否在1-10**9 if (0 <= int_a <= 9) & (0 <= int_b <= 9) & (1 <= int_n <= (10**9)): # 3、计算(a ** n) * b的值 superdata = str((int_a**int_n) * int_b) print("superdata=", superdata) # 4、对求得值进行切分并相加 for data in superdata: data_sum = data_sum + int(data) print("data_sum:", data_sum) print("最终data_sum = ", data_sum) else: print("您输入的值超过了所要求的整数范围!!!") """ import time a = 3 b = 4 n = 3 def sum_superdata(a, b, n): data_sum = 0 try: int_a = int(a) int_b = int(b) int_n = int(n) except: print("您输入的数值中包含非整数值!!!") if (0 <= int_a <= 9) & (0 <= int_b <= 9) & (1 <= int_n <= (10 ** 9)): superdata = str((int_a ** int_n) * int_b) print("superdata=", superdata) for data in superdata: data_sum = data_sum + int(data) print("data_sum:", data_sum) print("最终data_sum = ", data_sum) else: print("您输入的值超过了所要求的整数范围!!!") # sum_superdata(1, 9, 100) if __name__ == "__main__": start = time.time() sum_superdata(a, b, n) end_start = time.time() print("sum_time = ", format(end_start-start))