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') while (line = readline()) {
const regOne = '(([0-9])|([1-9][0-9])|([1][0-9]{2})|([2](([0-4][0-9])|([5][0-5]))))'
print(new RegExp(`^(${regOne}\\.){3}(${regOne})$`).test(line)? "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 sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
System.out.println(isValidIp(s) ? "YES" : "NO");
}
sc.close();
}
private static boolean isValidIp(String input) {
String[] ss = input.split("\\.");
if (ss.length != 4) return false;
for (String s : ss) {
int a = Integer.parseInt(s);
if (a < 0 || a > 255) return false;
}
return true;
}
} #include <iostream>
#include <sstream>
#include <string>
using namespace std;
bool is_valid(string &s) {
stringstream ss(s);
int count = 0;
string t;
while (getline(ss, t, '.')) {
count++;
long val = stoi(t, 0, 10);
if (val < 0 || val > 255) return false;
}
if (count != 4) return false;
return true;
}
int main() {
string s;
while (cin >> s) {
cout << (is_valid(s) ? "YES" : "NO") << endl;
}
return 0;
} #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int is_valid_ip(char *s) {
int total = 0;
char *p = strtok(s, " ");
while (p) {
int val = atoi(p);
if (val < 0 || val > 255)return 0;
p = strtok(NULL, " ");
total++;
}
if (total != 4) return 0;
return 1;
}
int main() {
char s[4096] = {0};
while (gets(s)) {
printf(is_valid_ip(s) ? "YES\n" : "NO\n");
}
return 0;
} 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");
}
}
}
}