def findmax(s,n):
if n==1:
return s[0]
else:
if s[n-1]>findmax(s,n-1):
return s[n-1]
else:
return findmax(s,n-1)
s=list(map(int,input('请输入一个序列:').split(' ')))
findmax(s,len(s))
#计算调和函数1/n 的前n项和
def function(n):
if n==1:
return 1/n
else:
return function(n-1)+1/n
function(3)
def function(data,m,n):
if m==len(data)-1:
return data[m]
else:
return function(data,m+1,n-1)+data[m]*pow(10,n-1)
data=[1,2,3]
function(data,0,len(data))
import numpy as np
def function(A):
k=len(A)
try:
while k/2 !=0:
k=k/2
except:
raise TypeError('输入的序列个数有误,个数必须是2的幂次方')
B=np.zeros((1,len(A)//2))
if len(A)==1:
B[0,0]=A[0]
print(B[0])
else:
for i in range(len(A)//2):
B[0,i]=A[2*i]+A[2*i+1]
return np.sum(B,1)
A=[1,2,3,4]
function(A)
def findmax(A,n):
if n==1:
return A[0]
else:
if A[n-1]>findmax(A,n-1):
return A[n-1]
else:
return findmax(A,n-1)
def findmin(A,n):
if n==1:
return A[0]
else:
if A[n-1]>findmin(A,n-1):
return findmin(A,n-1)
else:
return A[n-1]
a=list(map(int ,input('请输入一个序列:').split(' ')))
print(findmin(a,len(a)),findmax(a,len(a)))
def function(m,n):
if n==1 :
return m
else:
return function(m,n-1)+m
function(4,5)
def function(a):#将字符串逆序输出
if len(a)==1:
return str(a[0])
else:
return function(a[1:])+a[0]
a=input('请输入一个字符串:')
function(a)
def function(a):#将字符串逆序输出
if len(a)==1:
return str(a[0])
else:
return function(a[1:])+a[0]
a=input('请输入一个字符串:')
function(a)
if function(a)==a:
print(True)
else:
print(False)
def function(a,n,count1=0,count2=0):
b=['a','e','i','o','u','A','E','I','O','U']
if n==1:
if a[0] in b:
count1+=1
else:
count2+=1
return '元音字母个数是 {} ,辅音字母个数是 {}'.format(count1,count2)
else:
if a[0] in b:
count1+=1
return function(a[1:],n-1,count1,count2)
else:
count2+=1
return function(a[1:],n-1,count1,count2)
a=input('请输入只含字母的序列:\n')
n=len(a)
function(a,n,count1=0,count2=0)
有奇数值的前面;
def function(a,m,b='',c=''):
if m==1:
if int(a[0])%2==0:
b+=a[0]
else:
c+=a[0]
return int(b+c)
else:
if int(a[0])%2==0:
b+=a[0]
return function(a[1:],m-1,b,c)
else:
c+=a[0]
return function(a[1:],m-1,b,c)
function('846536',6)