算法题目-网易2018

 问题1:给定一个N*M矩阵,每个位置一张牌一开始正面朝上,按位置反转一次,并且周围8张牌也一起反转,最后统计背面朝上的牌数。

x=int(input())
l=[]
for i in range(x):
    y=input()
    y=y.split(' ')
    y[0]=int(y[0])
    y[1]=int(y[1])
    l.append(y)
def find(a):
    N=a[0]
    M=a[1]
    if N==1 and M==1:
        print(1)
    elif  N==2 or M==2:
        print(0)
    elif M==1 and N>1:
        print(N-2)
    else:
        print((N-2)*(M-2))
for i in range(x):
    find(l[i])

问题2:输入一字符串比如:wbwbbw,输出最长连续wb交叉长度,此例输出为4,字符串可进行切断重连,比如wbwwb,可在两个连续w之间切断,构成新串wbwbw,最长长度为5

s=input()
cou = 1
re=[]
ma=0
start=1
end=0
for i in range(1,len(s)):
    if s[i]!=s[i-1]:
        cou+=1
        if cou==len(s):
            start=cou
        elif i == len(s) - 1:
            end = cou
    else:
        if i == len(s) - 1:
            end=1
        if i==cou:
            start=cou
        elif cou>ma:
            ma=cou
        cou=1
re.append(start)
re.append(ma)
re.append(end)
print(re)
ma=max(re)
if s[0]!=s[-1] and end+start>ma:
    ma=start+end
print(ma)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值