1.战神的对称谜题
不知道为什么超时!
def main():s = input()result = 0for i in range(len(s)):l = i - 1r = i + 1while l >= 0 and r < len(s) and s[l] == s[r]:result = max(result, r - l + 1)l -= 1r += 1l = ir = i + 1while l >= 0 and r < len(s) and s[l] == s[r]:result = max(result, r - l + 1)l -= 1r += 1print(result)if __name__ == '__main__':main();
2.元音字母
def main():s = input()n = int(input())vowel = ["a", "e", "i", "o", "u"]# print(s)result = 0for i in range(n):if s[i] in vowel:result += 1l = 0r = n - 1tmp = resultwhile r + 1 < len(s):if s[l] in vowel and s[r + 1] not in vowel:tmp -= 1if s[r + 1] in vowel and s[l] not in vowel:tmp += 1if tmp > result:result = tmpl += 1r += 1print(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 = 1r = 2while 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 += 1r += 1continueelif sum < X:r += 1continueelse:l += 1continuefor i in result:for j in i:print(j,end=" ")print("")if __name__ == '__main__':main();