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++
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();
原文地址:https://blog.csdn.net/weixin_53908842/article/details/134739309
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_27120.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。