题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
from operator import ipow
'''合法IP:由 . 分成4段
除了. 只有数字
每段不由0开头,且0<=数字<=255(8位二进制数最大值,转为十进制是225)
s='1'*8
print(int(s,2)) # 11111111 转成十进制是225
'''
ip=input().split(".")
if len(ip)!=4: # 若长度不等于4,直接输出NO,后续语句不用执行
print("NO")
else:
for i in ip:
if (
not i.isdigit()
or i==''
or int(i)<0
or int(i)>255
or (i[0]=='0' and len(i)>1)
):
print("NO")
break
else:
print("YES")
联想公司福利 1481人发布
查看18道真题和解析