最大最小猴子吃桃的Python程序
求最大值、最小值,解猴子吃桃问
题
快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题
的
Python程序
ylzhao 2013.12.5
1.求数组元素的最大值
解法1 非递归法
#求最大值的函数,非递归
def Max(A,n):
maxdata=A[0]
for i in range(1,n):
if A[i]>maxdata:
maxdata=A[i]
return maxdata
#调用函数,求序列中的最大值
while(1): #循环
n=int(input("请输入元素个数:")) #输入元素个数
if n<1: #个数小于0结束
break
else: #个数大于0输入元素
a=[] #开始列
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
为空
for i in range(0,n): #循环输入n个数
x=float(input("请输入第"+str(i+1)+"元素")) #输入
a.append(x) #添加到列表中
print("它们的最大值为:",Max(a,n)) #求最大值并显示
print("") #换行
解法2 一般递归法
#求最大值的函数,一般递归
def Max(A,n):
if n==1:
return A[0] #一个元素时直接返回
else:
field is downwind, brooding House separate from the breeding shed, and ... Building chicken farms must also consider the construction of chicken manure treatment facilities such as septic tanks, methane-generating pit, do not let the chicken manure directly. Five to do a disease prevention and control, do not cut corners. Built into the chicken in chicken farm, will be required to do a variety of vaccine injections and disinfection in chicken farm, do not hold the chance, and do scientific farming. Reasonable problems in building a chicken coop hen house layout, can provide a good environment for chicken, making its full productive potential, so other than in understanding the physiological characteristics of the chicken itself, and must be properly planned and constructed sheds. A chicken layout considerations 1. Sites to choose away from populated areas, traffic convenient, away from the road 2. Gaozao terrain, a lot of sunshine. Winter sun as possible, summer wind, and not after the rain water. Larger, leaving room for development 3. Abundant water resources pollution-free, easy to access, sufficient power is guaranteed 4. Building structure, the economy, saving money, and saving energy, it is facing in accordance with local environmental and physiological condition, lighting is good, easy to ventilation, easy to operate, so conducive to cooling in the summer, to insulation in winter cold 5. Layout of premises should be reasonable, do distinguish between
m=Max(A,n-1) #前面n-1个元素的最大
if m
A[n-1]: #和最后一个元素比较
return A[n-1] #返回最小的
else:
return m #调用函数,求序列中的最小值
while(1): #循环
n=int(input("请输入元素个数:")) #输入元素个数
if n<1: #个数小于0结束
break
else: #个数大于0输入元素
a=[] #开始列表为空
for i in range(0,n): #循环输入n个数
x=float(input("请输入第"+str(i+1)+"元素")) #输入
a.append(x) #添加到列表中
print("它们的最小值为:",Min(a,n)) #求最小值并显示
print("") #换行
field is downwind, brooding House separate from the breeding shed, and ... Building chicken farms must also consider the construction of chicken manure treatment facilities such as septic tanks, methane-generating pit, do not let the chicken manure directly. Five to do a disease prevention and control, do not cut corners. Built into the chicken in chicken farm, will be required to do a variety of vaccine injections and disinfection in chicken farm, do not hold the chance, and do scientific farming. Reasonable problems in building a chicken coop hen house layout, can provide a good environment for chicken, making its full productive potential, so other than in understanding the physiological characteristics of the chicken itself, and must be properly planned and constructed sheds. A chicken layout considerations 1. Sites to choose away from populated areas, traffic convenient, away from the road 2. Gaozao terrain, a lot of sunshine. Winter sun as possible, summer wind, and not after the rain water. Larger, leaving room for development 3. Abundant water resources pollution-free, easy to access, sufficient power is guaranteed 4. Building structure, the economy, saving money, and saving energy, it is facing in accordance with local environmental and physiological condition, lighting is good, easy to ventilation, easy to operate, so conducive to cooling in the summer, to insulation in winter cold 5. Layout of premises should be reasonable, do distinguish between
解法2二分法递归
#二分法递归求最小值的函数
def Min(A,n):
if n==1:
return A[0]
else:
mid=n//2 #中间元素的位置(下标)
leftA=A[:mid] #左边子序列
rightA=A[mid:] #右边子序列
leftMin=Min(leftA,mid) #递归求左边的最小
rightMin=Min(rightA,n-mid) #递归求右边的最小
if leftMin>rightMin: #比较左右子序列的最小值
return rightMin #返回较小的
else:
return leftMin #调用函数,求序列中的最小值
while(1): #循环
n=int(input("请输入元素个数:")) #输入元素个数
if n<1: #个数小于0结束
break
else: #个数大于0输入元素
a=[] #开始列表为空
for i in range(0,n): #循环输入n个数
x=float(input("请输入第"+str(i+1)+"元素")) #输入
a.append(x) #添加到列表中
print("它们的最小值为:",Min(a,n)) #求最小值并显示
print("") #换行
field is downwind, brooding House separate from the breeding shed, and ... Building chicken farms must also consider the construction of chicken manure treatment facilities such as septic tanks, methane-generating pit, do not let the chicken manure directly. Five to do a disease prevention and control, do not cut corners. Built into the chicken in chicken farm, will be required to do a variety of vaccine injections and disinfection in chicken farm, do not hold the chance, and do scientific farming. Reasonable problems in building a chicken coop hen house layout, can provide a good environment for chicken, making its full productive potential, so other than in understanding the physiological characteristics of the chicken itself, and must be properly planned and constructed sheds. A chicken layout considerations 1. Sites to choose away from populated areas, traffic convenient, away from the road 2. Gaozao terrain, a lot of sunshine. Winter sun as possible, summer wind, and not after the rain water. Larger, leaving room for development 3. Abundant water resources pollution-free, easy to access, sufficient power is guaranteed 4. Building structure, the economy, saving money, and saving energy, it is facing in accordance with local environmental and physiological condition, lighting is good, easy to ventilation, easy to operate, so conducive to cooling in the summer, to insulation in winter cold 5. Layout of premises should be reasonable, do distinguish between
第3题 猴子吃桃问题
def monkeypeach(n): #函数
if n==10: #第10天的桃子数,是确定的
return 1 #第10天的桃子数是1,计算结果为1,返回1
else: #计算的不是第10天
tmp=2*(monkeypeach(n+1)+1) #等于后一天的桃子数加1再乘2
return tmp #返回计算结果 print("本程序求解猴子吃桃问题")
n=int(input('请输入天数([1,10],超出范围程序结束):')) while(n>0 and n<11):
num=monkeypeach(n) #调用函数得到计算结果
print('第',n,'天桃子的数量为:',num)
n=int(input('请输入天数:'))
print("程序结束")
field is downwind, brooding House separate from the breeding shed, and ... Building chicken farms must also consider the construction of chicken manure treatment facilities such as septic tanks, methane-generating pit, do not let the chicken manure directly. Five to do a disease prevention and control, do not cut corners. Built into the chicken in chicken farm, will be required to do a variety of vaccine injections and disinfection in chicken farm, do not hold the chance, and do scientific farming. Reasonable problems in building a chicken coop hen house layout, can provide a good environment for chicken, making its full productive potential, so other than in understanding the physiological characteristics of the chicken itself, and must be properly planned and constructed sheds. A chicken layout considerations 1. Sites to choose away from populated areas, traffic convenient, away from the road 2. Gaozao terrain, a lot of sunshine. Winter sun as possible, summer wind, and not after the rain water. Larger, leaving room for development 3. Abundant water resources pollution-free, easy to access, sufficient power is guaranteed 4. Building structure, the economy, saving money, and saving energy, it is facing in accordance with local environmental and physiological condition, lighting is good, easy to ventilation, easy to operate, so conducive to cooling in the summer, to insulation in winter cold 5. Layout of premises should be reasonable, do distinguish between
测试结果
field is downwind, brooding House separate from the breeding shed, and ... Building chicken farms must also consider the construction of chicken manure treatment facilities such as septic tanks, methane-generating pit, do not let the chicken manure directly. Five to do a disease prevention and control, do not cut corners. Built into the chicken in chicken farm, will be required to do a variety of vaccine injections and disinfection in chicken farm, do not hold the chance, and do scientific farming. Reasonable problems in building a chicken coop hen house layout, can provide a good environment for chicken, making its full productive potential, so other than in understanding the physiological characteristics of the chicken itself, and must be properly planned and constructed sheds. A chicken layout considerations 1. Sites to choose away from populated areas, traffic convenient, away from the road 2. Gaozao terrain, a lot of sunshine. Winter sun as possible, summer wind, and not after the rain water. Larger, leaving room for development 3. Abundant water resources pollution-free, easy to access, sufficient power is guaranteed 4. Building structure, the economy, saving money, and saving energy, it is facing in accordance with local environmental and physiological condition, lighting is good, easy to ventilation, easy to operate, so conducive to cooling in the summer, to insulation in winter cold 5. Layout of premises should be reasonable, do distinguish between