首页 > 试题广场 >

IP地址

[编程题]IP地址
  • 热度指数:17793 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入一个ip地址串,判断是否合法。

输入描述:
每行有一个IP地址,IP地址的形式为a.b.c.d,其中a、b、c、d都是整数。


输出描述:
可能有多组测试数据,对于每组数据,如果IP地址合法则输出"Yes!”,否则输出"No!”。

合法的IP地址为:
a、b、c、d都是0-255的整数。
示例1

输入

255.255.255.255
512.12.2.3

输出

Yes!
No!
print("Yes!" if [int(item)>=0 and int(item)<=255 for item in input().split('.')].count(True)==4 else "No!")

发表于 2021-01-31 11:14:58 回复(0)
while True:
    try:
        IP = map(int,input().split('.'))
        for each in IP:
            if not 0<=each <=255:
                print('No!')
                break
        else:
            print('Yes!')
    except:
        break

发表于 2018-07-05 01:51:46 回复(0)

python三行代码搞定。

while True:
    try:
        a = int(input())
        for i in range(a):
            print("Yes!" if sum(map(lambda c: 0 <= c <= 255, map(int, input().split(".")))) == 4 else "No!")

    except:
        break
发表于 2017-10-04 08:54:12 回复(7)
def fun2():
    ips = [raw_input() for _ in range(input())]
    for ip in ips:
        print 'Yes!' if all(map(lambda x: 0 <= int(x) <= 255, ip.split('.'))) else 'No!'


while True:
    try:
        fun2()
    except EOFError:
        break

编辑于 2017-01-03 21:00:51 回复(0)