HDU2206: IP Address Calculation

Source: Internet
Author: User

Problem Description I learned a lot about IP addresses in my online courses. IP is short for Internet Protocol. Sometimes we use IP to refer to our IP address. Now an IPV4 address is represented by a 32-bit unsigned integer, which is usually displayed in the dot-division mode, the IP address is divided into four parts, each of which is an 8-bit, expressed as an unsigned integer (so you do not need to use a positive number). For example, 192.168.100.16 is an IP address that we are very familiar, there is no space in an IP address string (because it must be expressed as a 32 number ). Www.2cto.com but careless, I often write an error in the IP address. Now you need to use a program to determine. Input has multiple cases. Each case has a row and cannot exceed 100 characters. Output determines whether the Entered IP address is correct for each case. If YES is entered correctly, otherwise NO is entered. Sample Input192.168.100.16 Sample OutputYES [cpp]/* I have seen this question before, but I still don't know how to judge what the IP address is. I don't know until today I have referred to other people's code, the IP address must meet the following conditions: 1. '. 'There are only three 2. four positions with numbers. The length should be 1 ~ 3 (including boundaries) 3. Each number should be: 1 ~ 255 (including boundaries) 4. special characters */# include "stdio. h "# include" string. h "int main () {int flag; char str [111]; int temp; int I, l; int place [10], k; while (gets (str )! = 0 ){//'. 'There can be only three places [0] =-1; k = 1; temp = 0; for (I = 0; str [I]; I ++) if (str [I] = '. ') {temp ++; place [k] = I; k ++;} if (temp! = 3) {printf ("NO \ n"); continue;} place [k] = I; k ++; // NO special character flag = 0; for (I = 0; str [I]; I ++) {if (! (Str [I] = '. ') | ('0' <= str [I] & str [I] <= '9') {printf ("NO \ n "); flag = 1; break; }}if (flag = 1) continue; // four positions with numbers. The length should be 1 ~ 3 (including boundaries) flag = 0; for (I = 1; I <k; I ++) {if (place [I]-place [I-1] = 1 | place [I]-place [I-1]> 4) {printf ("NO \ n "); flag = 1; break; }}if (flag = 1) continue; // each number should be: 1 ~ 255 (with boundary) flag = 0; for (I = 1; I <k; I ++) {temp = 0; for (l = place [I-1] + 1; l <place [I]; l ++) {temp * = 10; temp + = str [l]-'0';} if (temp> = 256) {printf ("NO \ n"); flag = 1; break ;}} if (flag = 1) continue; printf ("YES \ n ");} return 0 ;}

Related Article

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.