#第三章
3.1 穷举法
我们枚举所有可能性,直到得到准确答案或者尝试完所有的值。它是一种查找技术,只有在被查找集合中包含答案时才有效。
练习题:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwt,满足o<pwt<6,并且root**pwr等于用户输入的整数。如果不存在这样的一对数,则输出一条信息进行说明。
x = int(input("Enter an integer: "))
cout = 0
for i in range(1,6):
pwr = i
root = 0
while root**pwr<abs(x):
root = root+1
if root**pwr == abs(x):
cout = 1
if x<0:
root = -root
print(root,pwr)
if cout == 0:
print(x," is not a perfect cube.")
3.2 for 循环
假设s是包含多个小数的字符串,由逗号隔开,如s=‘1.23,2.4,3.123’。编写一个程序,输出s中所有数值的和。
s=input("Enter the numbers splited by ',':")
cout = s