每行有一个IP地址,IP地址的形式为a.b.c.d,其中a、b、c、d都是整数。
可能有多组测试数据,对于每组数据,如果IP地址合法则输出"Yes!”,否则输出"No!”。 合法的IP地址为: a、b、c、d都是0-255的整数。
255.255.255.255 512.12.2.3
Yes! No!
情况自己可以多考虑一些
package com.speical.first;
import java.util.Scanner;
/**
* ip合理检测
*
* 1.范围正确
* 2.不能有数字和'.'以外的字符
* 3.段数为4
* @author Special
* @time 2018/02/08 16:08:05
*/
public class Pro203 {
private 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.next();
int num = 0, count = 0;
boolean flag = true;
for(int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if(isNum(ch)) {
num = 0;
while(i < str.length() && isNum(str.charAt(i))) {
num = num * 10 + str.charAt(i++) - '0';
}
if(i < str.length() && str.charAt(i) != '.'
||num < 0 || num >= 256) {
flag = false;
break;
}
count++;
}else if(ch != '.') {
flag = false;
break;
}
}
if(count < 4) { flag = false; }
System.out.println(flag ? "Yes!" : "No!");
}
}
}
#include<iostream> (720)#include<string> #include<algorithm> (831)#include<regex> using namespace std; int main() { string s; while (getline(cin,s)) { int count = 0; for (int i = 0; i < s.size(); i++) if (s[i] > '9' || s[i] < '0') count++; if (count != 3) cout << "No!"<< endl; else { int flag = 0; regex pattern("\\d+"); smatch result; string::const_iterator str = s.begin(); string::const_iterator str_end = s.end(); while (regex_search(str, str_end,result, pattern)) { string z=result[0]; int a = atoi(z.c_str()); if (a > 255) { cout << "No!" << endl; flag = 1; break; } str = result[0].second; } if (flag == 0) cout << "Yes!" << endl; } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); next: while (scanner.hasNext()) { String s = scanner.nextLine(); String[] ss = s.split("\\."); if (ss.length != 4) { System.out.println("No!"); break next; } else { for (String s1 : ss) { int i1 = Integer.parseInt(s1); if (!(i1 <= 255 && i1 >= 0)) { System.out.println("No!"); break next; } } System.out.println("Yes"); } } } }
import java.util.Scanner; import java.util.regex.Pattern; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); String content = sc.next(); //java中的"\"要+1 String pattern = "(((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|" + "(25[0-5]))\\.){3}" + "((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))"; boolean isMatch = Pattern.matches(pattern, content); if(isMatch){ System.out.println("Yes!"); }else { System.out.println("No!"); } } }
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scanr=new Scanner(System.in); String str=scanr.nextLine(); if (str.split("\\.").length!=4) System.out.println("No!"); else { for (int i = 0; i < 4; i++) { int value = Integer.parseInt(str.split("\\.")[i]); if (value < 0 || value > 255) { System.out.println("No!"); return; } } System.out.println("Yes!"); } } }
#include<stdio.h>
int main(){
char s[100];
gets(s);
int i=0;
int v=0;
int c=0;
while(s[i]!='\0'){
if(s[i]=='.')
c++;
i++;
}
if(i>15 || c!=3){
printf("No!");
return 0;
}
s[i]='.';
s[i+1]='\0';
i=0;
while(s[i]!='\0'){
while(s[i]!='.'){
v=v*10+(s[i]-'0');
i++;
}
if(v<0 || v>255){
printf("No!");
return 0;
}
v=0;
i++;
}
printf("Yes!");
return 0;
}
#include <cstdio> int main() { int ip[4]; int n; while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf("%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]); bool flag = true; for (int j = 0; j < 4; j++) { if (ip[j] < 0 || ip[j] > 255) { flag = false; break; } } if (flag) printf("Yes!\n"); else printf("No!\n"); } } return 0; }
<?php $char = fgets(STDIN); $result = array(); $i=0; while($i<$char){ fscanf(STDIN,"%d.%d.%d.%d",$a,$b,$c,$d); if($a<0||$b<0||$c<0||$d<0||$a>255||$b>255||$c>255||$d>255){ $result[$i]="No!"; }else{ $result[$i]="Yes!"; } $i++; } echo implode("\n",$result);
#include <stdio.h> #include <string.h> bool checkIP(char *s) { int numOfChar = strlen(s); if(numOfChar>15) // IP v4 0.0.0.0 ~ 255.255.255.255 return false; int i,dotAdd[3],dotCount = 0; for (i = 0; i < numOfChar; i++) // 找出.所在的下标 可以考虑边判断边动指针i { if (s[i] == '.') { dotCount++; if (dotCount > 3) return false; dotAdd[dotCount-1] = i; // 日常下标减一 常见错误 } } if (dotCount != 3) return false; int num, j; for (i = 0; i < dotCount; i++) // 只判断了.前面的符不符合要求,最后一个数字也要判断 { num = 0,j = 1; // 通过.的位置定位其前后的数字字符并转化为数字 int index = dotAdd[i]-1; // 定位字符串中的.对其前面的数进行转换和求和操作 index > 0 && s[index] != '.' while (index >= 0 && s[index] != '.') { num = num + j*(s[index] - '0'); j *= 10; index--; } if (num > 255) return false; } int lastDot = dotAdd[2]; num = 0, j = 1; while (s[numOfChar - 1] != '.') { numOfChar--; num = num + j*(s[numOfChar] - '0'); j *= 10; } if (num > 255) return false; return true; } int main() { char input[20]; int n; scanf("%d", &n); while (n--) { scanf("%s", input); bool a = checkIP(input); (a == false) ? printf("No!\n") : printf("Yes!\n"); } return 0; }
bool checkIP(char *s) { int index = strlen(s) - 1; if (strlen(s) > 15) // 0.0.0.0 ~ 255.255.255.255 return false; int i,dotCount = 0; for (i = 0; i <= index; i++) // 判断是否存在非法输入 { if (s[i] == '.') { dotCount++; continue; } else if (s[i] >= '0' && s[i] <= '9') continue; else return false; } if (dotCount != 3) // 合格的ip v4 地址有三个点 return false; // 使指针首先指向字符串的最后一个字符,从后往前扫是为了方便计算 while (1) { int num = 0, j = 1; while (s[index] != '.' && index >= 0) { num = num + (s[index] - '0') * j; j *= 10; index--; } index--; if (num > 255) return false; if (index < 0) break; } return true; } int main() { char input[20]; int n; scanf("%d", &n); while (n--) { scanf("%s", input); bool a = checkIP(input); (a == false) ? printf("No!\n") : printf("Yes!\n"); } return 0; }
#include<stdio.h> #include<string.h> int main(){ char a[20]; int i=0; gets(a); int sign=1; for(i=0;i<strlen(a);i++){ int sum=0; while(a[i]!='.'&&a[i]!='\0'){ sum=10*sum+a[i]-'0'; i++; } if(sum<0||sum>255){ sign=0; break; } } if(sign==1) printf("Yes!"); else printf("No!"); return 0; }
//代码独立开来; #include<iostream> #include<vector> #include<algorithm> #include<cmath> #include<stack> using namespace std; int strToInt(string str,int begin,int end) { int num=0; for(int i=begin;i<end;i++) { num=num*10+(str[i]-'0'); } return num; } bool Comp(int number) { return (number>=0 && number<=255); } bool isIpaddress(string str) { int x1=str.find('.',0); int x2=str.find('.',x1+1); int x3=str.find('.',x2+1); //cout<<x1<<" "<<x2<<" "<<x3<<endl; int y1=strToInt(str,0,x1); int y2=strToInt(str,x1+1,x2); int y3=strToInt(str,x2+1,x3); int y4=strToInt(str,x3+1,str.length()); //cout<<y1<<" "<<y2<<" "<<y3<<" "<<y4<<endl; if(Comp(y1)&&Comp(y2)&&Comp(y3)&&Comp(y4)) return true; else return false; } int main() { int N; string str; while(cin>>N) { for(int i=1;i<=N;i++) { cin>>str; if(isIpaddress(str)) cout<<"Yes!"<<endl; else cout<<"No!"<<endl; } } return 0; }
防止出现255.255.255.255.255类似的错误
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a,b,c,d;
string tmp;
scanf("%d.%d.%d.%d",&a,&b,&c,&d);
getline(cin,tmp);
if(a>=0&&a<=255 && b>=0&&b<=255 && c>=0&&c<=255 && d>=0&&d<=255 && tmp.empty())
{
printf("Yes!\n");
}
else
printf("No!\n");
return 0;
}
#include <iostream> using namespace std; int main(){ int n, a, b, c, d; char ch; //ch字符吸收输入中的'.' cin>>n; while(n--){ 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) printf("No!\n"); else printf("Yes!\n"); } return 0; }
#include<iostream> #include<string> #include<vector> using namespace std; bool isTrue(string& str){ int num = 0; for(char s : str){ if(s != '.'){ num *= 10; num += s - '0'; }else{ if(num > 255 || num < 0){ return false; } num = 0; } } if(num > 255 || num < 0){ return false; } return true; } int main(){ vector<string> ss; string t; while(cin>>t){ ss.push_back(t); t = ""; } for(int i = 0; i < ss.size(); i++){ if(isTrue(ss[i])){ cout<<"Yes!"<<endl; }else{ cout<<"No!"<<endl; } } }