IPV4地址可以用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此正号不需要出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
现在需要你用程序来判断IP是否合法。
数据范围:数据组数:
进阶:时间复杂度:
,空间复杂度:%5C)
IPV4地址可以用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此正号不需要出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
输入一个ip地址,保证不包含空格
返回判断的结果YES or NO
255.255.255.1000
NO
#include <bits/stdc++.h>
using namespace std;
bool judgeIP(string& s){
if(s.empty()) return false;
for(char ch : s){
if(!(ch >= '0' && ch <= '9')) return false;// 不是数字不行
}
int n = stoi(s);
string s2 = to_string(n);
if(s2.size() != s.size()) return false;
if(n > 255) return false;
return true;
}
int main() {
string str;
while(cin >> str){
vector<string> v;
int start = 0;
for(int i = 0; i <= str.size(); i ++){
if(i == str.size() || str[i] == '.'){
string temp = str.substr(start, i - start);
start = i + 1;
v.push_back(temp);
}
}
if(v.size() != 4){
cout << "NO" << endl;
}else{
bool isLegal = true;
for(string s : v){
if(judgeIP(s) == false){
isLegal = false;
break;
}
}
if(isLegal) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}
let ip = readline()
let ips = ip.split('.')
console.log(ips.length===4 && ips.every(el=> {
return parseInt(el).toString() === el && parseInt(el)<=255 && parseInt(el)>=0
})? 'YES' : 'NO') import java.util.Scanner;
/**
* 练习写个正则表达式
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String s = scanner.nextLine();
solution(s);
}
}
/**
* ip地址判断
* <p>
* 正则表达式
* 0-199: (0|1)?[0-9]{1,2}
* 200-249: 2[1-4][0-9]
* 250-255: 25[0-5]
* 0-255: (((0|1)?[0-9]{1,2})|(2[1-4][0-9])|(25[0-5]))
* ip: ((((0|1)?[0-9]{1,2})|(2[1-4][0-9])|(25[0-5]))\\.){3}(((0|1)?[0-9]{1,2})|(2[1-4][0-9])|(25[0-5]))
*
* @param ip
*/
public static void solution(String ip) {
boolean matches = ip.matches("((((0|1)?[0-9]{1,2})|(2[1-4][0-9])|(25[0-5]))\\.){3}(((0|1)?[0-9]{1,2})|(2[1-4][0-9])|(25[0-5]))");
System.out.println(matches ? "YES" : "NO");
}
} #include <bits/stdc++.h>
using namespace std;
int main(){
unsigned int a,b,c,d;
char ch;
while(cin >> a >> ch >> b >> ch >> c >> ch >> d){
if((a == 0 && b == 0 && c == 0 && d == 0) || (a == 255 && b == 255 && c == 255 && d == 255)){
cout << "NO" << endl;
} else if((a<0 || a > 255) || (b<0 || b > 255) || (c<0 || c > 255) || (d<0 || d > 255)){
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
} import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine()) != null){
String[] ip = line.trim().split("\\.");
boolean flag = true;
for(int i = 0; i < ip.length; i++){
int num = Integer.parseInt(ip[i]);
if(num < 0 || num > 255){
flag = false;
break;
}
}
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}
}
} python版 while True:
try:
for num in list(map(int, input().split('.'))):
if num < 0&nbs***bsp;num > 255:
print("NO")
break
else:
print("YES")
except:
break import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNextLine()){
String ip = s.nextLine();
boolean flag = true;
if(ip.contains(" "))flag = false;
else{
String[] ipPart = ip.split("\\.");
if(ipPart.length != 4)flag = false;
else{
for(int i = 0;i < ipPart.length;i ++){
if(Integer.parseInt(ipPart[i]) > 255 || Integer.parseInt(ipPart[i]) < 0)flag = false;
}
}
}
System.out.println(flag?"YES":"NO");
}
}
}
import java.util.Scanner;
public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String string = sc.nextLine(); String[] arrs = string.split("\\."); String illegal="YES";
for(int i=0;i<arrs.length;i++)
{
if(Integer.parseInt(arrs[i])>255||Integer.parseInt(arrs[i])<0)
{
illegal="NO";
break;
}
} System.out.println(illegal); } }
}
居然只要判断数字大小就过了
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner jin = new Scanner(System.in);
while (jin.hasNext()) {
String s = jin.nextLine();
boolean flag = true;
String[] strings = s.split("\\.");
for (int i = 0; i < strings.length; i++) {
int num = Integer.valueOf(strings[i]);
if (!(num >= 0 & num <= 255)) {
flag = false;
}
}
if (flag) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String line = sc.nextLine();
boolean flag = true;
if(line.contains(" ")) {
flag = false;
} else {
String[] ips = line.split("\\.");
for(int i=0; i<ips.length; i++) {
if(Integer.valueOf(ips[i])<0 || Integer.valueOf(ips[i])>255) {
flag = false;
break;
}
}
}
if(flag) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
} #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
#include <vector>
using namespace std;
int main()
{ int a[4]; char c[3]; while(cin>>a[0]>>c[0]>>a[1]>>c[1]>>a[2]>>c[2]>>a[3]) { if(c[0]=='.' && c[1]=='.' && c[2]=='.') { bool flag = true; for(int i=0;i<4;i++) if(a[i]<0 || a[i]>255) flag = false; if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } return 0;
} 其实情况太多了。。。
package com.special.spet;
import java.util.Scanner;
/**
*
* @author special
* @date 2017年12月9日 上午9:46:31
*/
public class Pro89 {
public static boolean isNum(char ch) { return ch >= '0' && ch <= '9'; }
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while(input.hasNext()){
String str = input.nextLine();
String[] ips = str.split("\\.");
if(ips.length != 4)
System.out.println("NO");
else{
boolean flag = false;
for(int i = 0; i < ips.length; i++){
int temp = 0;
int index = 0;
while(index < ips[i].length() && isNum(ips[i].charAt(index)))
temp = temp * 10 + (ips[i].charAt(index++) - '0');
if(index < ips[i].length() || temp > 255){ //若是某个段位出现非数字或者范围超出跳出
flag = true;
break;
}
}
if(!flag) System.out.println("YES");
else System.out.println("NO");
}
}
}
}
//stringstream 用完之后要清空,否则下次使用之前的值还是保存在那里 #include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
string str;
while(getline(cin,str)){
if(str.find(' ') != string::npos){//首先判断是否存在空格
cout<<"NO"<<endl;
continue;
}
stringstream ss;
int flag=0;
while(!str.empty()){//用‘.‘分割开,各个数字大小是否在限定范围内
int a,b;
a=str.find('.');
if(a!=-1){
ss<<str.substr(0,a);
ss>>b;
ss.clear();
if(b<0 || b>256){
cout<<"NO"<<endl;
flag=1;
break;
}
str=str.substr(a+1,str.length());
}
else{//最后一个无法分割,可以直接使用
ss<<str;
ss>>b;
ss.clear();
if(b<0 || b>256){
cout<<"NO"<<endl;
flag=1;
break;
}
str="";
}
}
if(flag==0)
cout<<"YES"<<endl;
}
return 0;
}