加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

IP计算_hdu_2206

发布时间:2020-12-14 02:05:57 所属栏目:百科 来源:网络整理
导读:题目链接:~~~ 用正则表达式写: import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;public class hdu_IP计算2206 {public static void main(String[] args) {Scanner sc= new Scanner(System.in);String s;while(sc

题目链接:~~~


用正则表达式写:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class hdu_IP计算2206 {

	public static void main(String[] args) {
		
		Scanner sc= new Scanner(System.in);
		String s;
		while(sc.hasNext()){
			s = sc.nextLine();
Pattern p = Pattern.compile("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
			Matcher m = p.matcher(s);
			if(m.find())
				System.out.println("YES");
			else
				System.out.println("NO");
		}

	}

}


C/C++:


#include<stdio.h>
int main(){
	char str[102],tmp[102],a[4],b[4],c[4],d[4];
	while(gets(str))
	{
		tmp[0]='';
		if(sscanf(str,"%3[0-9].%3[0-9].%3[0-9].%3[0-9]%s",a,b,c,d,tmp)==4&&!tmp[0])
		{
			int a,d;
			sscanf(str,"%d.%d.%d.%d",&a,&b,&c,&d);
			if(a<256&&b<256&&c<256&&d<256)
				puts("YES");
			else
				puts("NO");
		}
		else
			puts("NO");
	}
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读