IP的计算
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8527 Accepted Submission(s): 1686
Problem Description
在网络课程上,我学到了很多有关IP的知识。IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此不需要用正号出现),如192.168.100.16,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
但是粗心的我,常常将IP地址写错,现在需要你用程序来判断。
但是粗心的我,常常将IP地址写错,现在需要你用程序来判断。
Input
输入有多个case,每个case有一行,不超过100个字符。
Output
对于每个case,判断输入的IP是否正确,如果正确输入YES,否则NO。
Sample Input
192.168.100.16
Sample Output
YES
·需要注意的一点是: sscanf()读入整数数据时,如果数据为+12, 一样会正常读入!
·所以程序中我们需要对于输入IP地址中是否存在'+'单独进行一下判断,因为题目中明显提出,IP地址中不能出现'+'号。
AC:
//
// main.cpp
// 20150128
//
// Created by { on 15-1-28.
// Copyright () 2015年 songjs. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <queue>
#include <stack>
using namespace std;
#define N 10100
#define INF 1<<31
#define LL long long
char f[N];
int r(int n){
return n>=0&&n<=255 ? 1: 0;
}
int check(){
//判断f是否标准化
if(strlen(f)>15) return 0;
if(strchr(f,'+')) return 0;
int a,b,c,d;char t[101];
if(sscanf(f,"%d.%d.%d.%d%s",&a,&b,&c,&d,t)==4){
if(r(a) && r(b)&&r(c)&&r(d))
return 1; //符合规则
}
return 0;
}
int main(){
while(gets(f)){
if(!check()){
printf("NO\n");
}else printf("YES\n");
}
return 0;
}