| Describe |
Now IPV4 is represented by a 32-bit unsigned integer, which is typically displayed in a point-and-click manner, dividing the IP address into 4 parts, each of which is 8 bits, which is represented as an unsigned integer (so there is no need to use a plus sign). As 10.137.17.1, we are very familiar with the IP address, an IP address string no space appears (because to be represented as a 32 number). Now you need to use the program to determine whether IP is legitimate. |
| Knowledge points |
String, loop, list, queue, stack, find, search, sort, tree, graph, array, function, pointer, enumeration, bitwise operation, struct, union, file operation, recursive |
| Run time limit |
10M |
| Memory limit |
128 |
| Input |
Enter an IP address |
| Output |
Returns the result of the decision Yes or NO |
| Sample input |
10.138.15.1 |
| Sample output |
YES |
Personal Summary:
(1)the use of regular expressions in Java syntax, for ', ' to use ' \ \ '
(2) Note that the empty string "" and the difference between NULL, determine whether a string is an empty string with. Equals ("")
(3) Judgment in the title itself
- Note that the string array must be four in length, such as 1.1.1 is not legal
- And you can't have an empty string, that is. Not adjacent, such as 1.1. 1 's not legal.
- For each sub-string to determine whether the number is greater than or equal to 0, less than or equal to 255 of the numbers; I am using a single add, in fact, you can use Java Integer.parseint (string2);
(1, judging if there are three '. ') ; 2, determine whether the three points are not adjacent, 3, determine whether each part is a number, 4, determine whether each number is between 0 to 255. )
Programme one:
ImportJava.util.Scanner; Public classMain { Public Static voidMain (String[]args) {Scanner Scanner=NewScanner (system.in); String String=Scanner.nextline (); string[] Num=string.split ("\ \")); Booleanresult =true; if(num.length!=4) result =false; for(String string2:num) {intNo =Integer.parseint (string2); if(No>=0 && no<=255) {}Else{result=false; Break; }}if(Result) {System.out.println ("YES"); }Else{System.out.println ("NO"); } }
}
Scenario Two:
ImportJava.util.Scanner; Public classip_useful { Public Static voidMain (string[] args) {System.out.println ("Please enter the IP address according to the specification"); Scanner Scstr=NewScanner (system.in); String sqy=Scstr.nextline (); System.out.println (judge (sqy)); //Print } Public Staticstring judge (String mxf) {string str[]=mxf.split ("\ \");//to separate a number of strings into dots if(str.length!=4){ return"NO";//If there is not exactly three '. ' will return } intNum=0; intTmp=0; intFlag[] =New int[Str.length]; intFlag1=1; for(inti=0;i<str.length;i++){ if(Str[i].equals ("")){ return"NO";//if an empty string is returned } Char[] Ch=str[i].tochararray ();//convert a single string to a character array if(ch.length<=3) {//less than four digits to be effective for(intj=0;j<ch.length;j++){ if(ch[j]>= ' 0 ' &&ch[j]<= ' 9 ') {//Digital Judgmenttmp=ch[j]-' 0 '; Num=num+tmp*10^ (ch.length-j-1); } } if(num>=0&&num<256) {Flag[i]=1; } } } for(intk=0;k<str.length;k++) {Flag1=FLAG1*FLAG[K];//as long as each one of the mark bit is 1; } if(flag1==1) return"YES"; Else return"NO"; }}
HW-IP legality _java