一维序列的严格极大值
上一个博客中问题的解答
这一次,只是做了一个小改动。
{a,b,c,d}
从头到尾一个一个的判断。
如果判断a不是极大值,那么无法确定b是不是,所以还需要判断b
如果判断b是极大值,那么a和c都不是,这样子,就可以跳过c,直接对d进行判断。
#Python在线编程
计算工具: bejson网站>后端>python3.8.1
import sys
import os
print("Hello,World")
import random
c = [random.randint(-9,9) for i in range(0,random.randint(1,10))]
#print(c)
#print(len(c))
def fone (c) :
lc = len(c)
y = [0]*lc
#print(y)
if lc==1:
y[0] = 1
else:
for k in range(0,lc):
if k==0 and c[k]>c[k+1] or k==lc-1 and c[k]>c[k-1] or k>0 and k<lc-1 and c[k]>c[k-1] and c[k]>c[k+1] :
y[k] = 1
k +=1 # 这就是那个所谓的跳过
#print(y)
return y
# 样例数据或者随机数据输入,进行计算观测
h = 'test'
print(h)
print(c)
print(fone(c))
print(len(c))
for z in range(0,4):
s = [random.randint(-10,8)]
f = fone(s)
print(s)
print(f)
print(len(s))
a=random.randint(-10,10)
for j in range(-99,99,29):
f=fone([a,j,23])
print(a,j,23)
print(f)
print(len(f))
x = [999,0]
fone(x)
print()
tt = [[-999],[-9.9],[0.00],[0],[0,1],[1,0],[1,1],[0,0],[999,685],[-9807,-77858],[-7078,-7078] ,
[9, 9, 9],[99,8,0],[0,8.99,80],
[8,9,0],[0,0,1],[0,1,1],[10,9,1000]]
print(tt)
print(len(tt))
for g in range(0,len(tt)):
d = fone(tt[g])
print(tt[g])
print(d)
print('位置',g,'元素(子矩阵)长度',len(tt[g]))
# 更改矩阵长度以观测更多情况。
for w in range(0,20):
cc = [random.randint(-20,20) for l in range(0,20)]
u = fone(cc)
print('the matrix cc :',cc)
print('the matrix u :',u)
print(len(u))
对于上面代码的函数fone(),它的输入input和输出output结构是相同的,并且output本身也可以作为自定义函数fone的一个参数输入。
虽然在数学上似乎没有什么意义,除了证明一个小结论:
x是一个一维矩阵,若 f1 = fone(x), 则有 f1 ==(等于) fone(f1)