Huawei online test question [basic article]--Judge IP legality

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.