算法题目-网易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)

猜你喜欢

转载自blog.csdn.net/qq_41994006/article/details/82531495