1.战神的对称谜题
不知道为什么超时!
def main():
s = input()
result = 0
for i in range(len(s)):
l = i - 1
r = i + 1
while l >= 0 and r < len(s) and s[l] == s[r]:
result = max(result, r - l + 1)
l -= 1
r += 1
l = i
r = i + 1
while l >= 0 and r < len(s) and s[l] == s[r]:
result = max(result, r - l + 1)
l -= 1
r += 1
print(result)
if __name__ == '__main__':
main();
2.元音字母
def main():
s = input()
n = int(input())
vowel = ["a", "e", "i", "o", "u"]
# print(s)
result = 0
for i in range(n):
if s[i] in vowel:
result += 1
l = 0
r = n - 1
tmp = result
while r + 1 < len(s):
if s[l] in vowel and s[r + 1] not in vowel:
tmp -= 1
if s[r + 1] in vowel and s[l] not in vowel:
tmp += 1
if tmp > result:
result = tmp
l += 1
r += 1
print(result)
if __name__ == '__main__':
main();
3.
5.求和
解法一:暴力破解,果然超时
def main():
result =[]
X = int(input())
for i in range(1,X):
for j in range(i + 1, X):
sum = int((i + j)*(j - i + 1)/2)
# print(sum)
if sum == X:
# print(i,j)
tmp = []
tmp.append(i)
tmp.append(j)
result.append(tmp)
for i in result:
for j in i:
print(j,end=" ")
print("")
if __name__ == '__main__':
main();
解法二:我去看看视频解析
双指针法,设置l = 1,r = 2
<X r++
=X 输出 l++并且r++
>X l++
终止条件是l <= X/2
(!不知道为什么有一个例子过不去)
def main():
result =[]
X = int(input())
l = 1
r = 2
while l <= X/2:
sum = int((l + r) * (r - l + 1) / 2)
if sum == X:
tmp = []
tmp.append(l)
tmp.append(r)
result.append(tmp)
l += 1
r += 1
continue
elif sum < X:
r += 1
continue
else:
l += 1
continue
for i in result:
for j in i:
print(j,end=" ")
print("")
if __name__ == '__main__':
main();