题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
思路:排除非法,对合格部分计数
def check(ip): if len(ip) != 4: # 排除不是由4部分组成的IP return "NO" c = 0 # ip合格的部分计数 for i in ip: if not i.isdigit(): # 排除非数值的情况 return "NO" elif 0 <= int(i) <= 255 and i == str(int(i)): c += 1 if c < 4: return "NO" return "YES" ip = input().split(".") print(check(ip))