汽水瓶:
while True:
try:
n = int(input())
if n==0:
break
a = n
cnt=0
while n>1:
a = n//3 # 此时的a是新增的可以喝的汽水瓶数
cnt+=a
b = n%3 # 此时是余下的汽水瓶数
n = a+b
# if n<3:
# 只有剩下2个的时候
if n==2:
# cnt加一
cnt+=1
break
print(cnt)
except:
break
字符串加解密:
while True:
try:
res1 = []
res2 = []
lst1 = list(input())
lst2 = list(input())
def encode(lst):
for i in lst:
if i.isdigit():
res1.append(str((int(i)+1)%10)) # 9+1->0
if i.isalpha():
if i.islower():
char = chr(ord(i)+1) if ord(i)+1<123 else "a"
res1.append(char.upper())
else:
char = chr(ord(i)+1) if ord(i)+1<91 else "A"
res1.append(char.lower())
return "".join(res1)
def decode(lst):
for i in lst:
if i.isdigit():
res2.append(str((int(i)-1)%10)) # 1-1
if i.isalpha():
if i.islower():
char = chr(ord(i)-1) if ord(i)-1>96 else "z"
res2.append(char.upper())
else:
char = chr(ord(i)-1) if ord(i)-1>64 else "Z"
res2.append(char.lower())
return "".join(res2)
print(encode(lst1))
print(decode(lst2))
except:
break
整数与IP地址间的转换:
while True:
try:
# ip to num
ip = input()
ip_list = ip.split(".") # ["10","0","3","193"]
ip2num = str()
for i in ip_list:
# bin()返回的是字符串
a = bin(int(i))[2:] # 把"0b"去掉
a = "0"*(8-len(a))+a if len(a)<8 else a
ip2num +=a
# 将二进制转换为十进制
print(int(ip2num,2))
# num to ip
num = input()
num2ip = []
num2 = bin(int(num))[2:]
num2 = "0"*(32-len(num2))+num2 if len(num2)<32 else num2
for i in range(4):
b = num2[8*i:8*(i+1)]
# int(a, 2/8/10/16) 将2进制/8进制/10进制/16进制数a转换为十进制数
b = str(int(b,2))
num2ip.append(b)
print(".".join(num2ip))
except:
break
统计字符:
from collections import Counter
while True:
try:
a = input()
cnt = Counter(a)
cnt_item = cnt.items()
a,b,c,d = 0,0,0,0
for item, num in cnt_item:
if item==" ":
b+=num
elif item.isalpha():
a+=num
elif item.isdigit():
c+=num
else:
d+=num
print(a)
print(b)
print(c)
print(d)
except:
break