算法刷题记录
暑期算法刷题记录
希望能够有所改变!
1、数位递增的数
# 解法一
n = int(input())
ans = 0
for i in range(1, n):
x = str(i)
for j in range(len(x)-1):
if x[j] > x[j+1]:
break
else:
ans += 1
print(ans)
# 解法二
import sys
num = 0
n = int(input())
for i in range(1, n+1):
c = i
b = sys.maxsize # 获取最大整数值sys.maxsize
while c > 0:
a = c % 10
if a > b:
break
b = a
c //= 10 # 使用整数除法
if c == 0:
num += 1
print(num)
2、凯撒加密
c = input() # 直接读取输入
new_c = ""
for char in c:
# 计算下一个字符的ASCII值,并确保在有效的ASCII字符范围内
new_char = chr((ord(char)-97+3)%26+97)
new_c += new_char
print(new_c)
3、最大距离
import os
import sys
n = int(input())
s = list(map(int,input().split()))
max_num = 0
for i in range(n-1):
for j in range(i+1,n):
max_num = max(max_num,abs(j-i) + abs(s[j]-s[i]))
print(max_num)
4、反倍数
import os
import sys
n = int(input())
s = list(map(int,input().split()))
ans = 0
for i in range(1,n+1):
if i%s[0]!=0 and i%s[1]!=0 and i%s[2]!=0:
ans += 1
print(ans)
5、洁净数
# 不太聪明的版本,这里会运行超时
import os
import sys
n= int(input())
ans=0
for i in range(1,n+1):
i_str = str(i)
while len(i_str)>0:
if len(i_str) == 1 and int(i_str) != 2:
ans += 1
a=int(i_str[-1]) # 取个位数
if a == 2:
break
else:
i_str = i_str[:-1] # 去掉最后一位数
print(ans)
# 参考别人的版本,使用到了判断字符或字符串是否在目标中
import os
import sys
n=int(input())
count=1
for i in range(1,n):
if "2" in str(i):
continue
else:
count=count+1
print(count)
6、确定字符串是否包含唯一字符
# 解法一:使用字典来计算各个字符出现的次数,再单独使用一个循环来判断是否有不唯一的字符出现
c = input().strip()
n = len(c)
count = {} # 初始化计数器字典
for i in range(n):
if c[i] in count:
count[c[i]] += 1
else:
count[c[i]] = 1
# 检查是否有重复的字符
if any(value > 1 for value in count.values()):
print("NO")
else:
print("YES")
# 解法二:set(s) 将字符串 s 转换成一个集合,集合中的元素是唯一的,这意味着所有重复的字符都会被移除
s=input()
if len(set(s))==len(s):
print('YES')
else:
print('NO')
# 解法三:类似解法一,换了一种表达方式,使用列表来存储数据
s = input()
s = s.upper()
d = []
for i in s:
if i not in d:
d.append(i)
else:
print("NO")
break
else:
print("YES")
# 解法四:从第一个字符开始判断字符串中有几个该字符出现
# 使用到了str.count(sub)方法来计算子字符串sub在字符串s中出现的次数
s = input()
for word in s:
if s.count(word) != 1:
print("NO")
break
if word == s[-1]:
print("YES")
7、确定字符串是否是另一个的排列
# 解法一:使用字段来统计字符出现的次数,字典的键是字符,值是出现的次数
s1 = input()
s2 = input()
count1 = {}
count2 = {}
for i in s1:
if i not in count1:
count1[i]=1
else:
count1[i]+=1
for j in s2:
if j not in count2:
count2[j]=1
else:
count2[j]+=1
if count1==count2:
print("YES")
else:
print("NO")
# 解法二:将输入转换为列表,并使用sort()方法进行排序,确保列表中的字符按照升序排列,然后比较两个列表是否完全相同即可
import os
import sys
s1 = list(input())
s2 = list(input())
s1.sort()
s2.sort()
if s1 == s2:
print("YES")
else:
print("NO")
# 解法三:确保两个字符串的长度一定相同,并且每个字符出现的次数也相同,同样可以保证两个字符串是一致的
import os
import sys
a=input()
b=input()
for i in a:
if a.count(i)!=b.count(i) or len(a)!=len(b):
s='NO'
break
else:
s='YES'
print(s)
8、压缩字符串
import os
import sys
s=input()
count={}
for i in s:
if i not in count:
count[i] = 1
else:
count[i]+=1
ss=""
for i in count:
if count[i]>1: # 如果字符出现次数大于1
ss += i + str(count[i])
else:
ss += i # 若只出现一次,那么数字1可以省略
if len(ss)<len(s): # 如果输入的字符串可以压缩,输出压缩的字符串
print(ss)
else:
print("NO") # 否则输出NO
但是上述解法不具备普遍性,比如:
测试 AAABBWWWWFFAAAAAAAVVQQQQER 输出是 A10B2W4F2V2Q4ER 但是正确答案是 A3B2W4F2A7V2Q4ER
# 优化版
import os
import sys
s = input()
compressed = ""
count = 1
for i in range(len(s)):
# 如果当前字符与下一个字符相同,则增加计数----使用这种思路就可以解决上面提到的问题了
if i < len(s) - 1 and s[i] == s[i + 1]:
count += 1
else:
# 否则,将字符和计数添加到压缩字符串中
if count > 1:
compressed += s[i] + str(count)
else:
compressed += s[i]
count = 1 # 重置计数
if len(compressed) < len(s):
print(compressed)
else:
print("NO")
9、Fizz Buzz经典问题
import os
import sys
n = int(input())
if n % 3==0:
if n % 5==0:
print("FizzBuzz")
else:
print("Fizz")
elif n % 5==0:
print("Buzz")
else:
print(n)