2021-01-02

一维序列的严格极大值

上一个博客中问题的解答
这一次,只是做了一个小改动。
{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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值