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.
Determine if it is legal ip#include<iostream> #include <string> #include <vector>using namespace Std;int main () { String Str;getline (CIN,STR); char ch;unsigned flag=0,k=0,value=0;vector<int> Ivec (3,0); for (int i=0;i!=str.size (); ++i) {ch=str[i];if (ch!= '. ') {ivec[k]=ch-48;k=k+1;if (i+1!=str.size () && str[i+1]== ') {if (k==1) {value=ivec[0];i=i+1;} if (k==2) {value=ivec[0]*10+ivec[1];i=i+1;} if (k==3) {value=ivec[0]*100+ivec[1]*10+ivec[2];i=i+1;} if (value<256) {flag++;k=0;} if (value>255) {break;} Vector<int> Ivec (3,0);} if (I+1==str.size ()) {if (k==1) {value=ivec[0];} if (k==2) {value=ivec[0]*10+ivec[1];} if (k==3) {value=ivec[0]*100+ivec[1]*10+ivec[2];} if (value<256) {flag++;k=0;break;}}}} if (flag==4) {cout<< "YES" <<ENDL;} elsecout<< "NO" <<endl;system ("PAUSE"); return 0;}
Huawei online test question [basic article]--Judge IP legality