“树人杯”暨第三届辽宁科技大学校园程序设计竞赛正赛D IP检测(绿)

Description

这天,树人的许老师给他们的学生讲了关于IP的知识,然后给他们一道题目让他们做。题目是这样的:我们知道IP地址由介于0-225间的数字组成,每个数字用“.”分开。比如192.168.0.1,点只有三个,用点隔开每个介于0-225的数字。我们常常会输错IP地址,现有一组IP地址让你根据所学的知识去检测它的对错。
通常IP的格式如: x.x.x.x (X大于等于0,小于等于255)你能帮他们解决这道题吗?

Input

有N组输入(0

Output

如果正确,输出"Correct",否则输出"Wrong".

Sample Input

5
192.168.0.1
369.168.0.1
225.225.225.0
127.0.0.0.1
127..0.1

 

Sample Output

Correct
Wrong
Correct
Wrong
Wrong

 

#include <stdio.h>
#include <string.h>
char ip[100];
int ic[100];
int geti(int x, int y)
{
int i, c;
if(y-x < 1)
return 256;
for(i=x, c=0; i<y; i++)
{
if(ip[i] < '0' || ip[i] > '9')
return 256;
c *= 10;
c += ip[i] -'0';
}
return c;
}
int parseIP()
{
int i=0, j, len, c;
len = strlen(ip);
for(i=j=c=0; ip[i]; ++i)
{
if(ip[i] == '.')
{ic[j++] = i;
c ++;}
}
if(c != 3)
return 1;
ic[3] = 100;
if (geti(0,ic[0])>255) return 1;
if (geti(ic[0]+1, ic[1])>255) return 1;
if(geti(ic[1]+1, ic[2])>255) return 1;
if (geti(ic[2]+1, len)>255) return 1;
return 0;
}
int main()
{
int n, i, j, x;
scanf("%d", &n);
while (n--)
{
scanf("%s", ip);
if( parseIP() ) puts("Wrong");
else puts("Correct");
}
return 0;
}

 

posted @ 2009-04-28 13:20  yansirfuture  阅读(213)  评论(0)    收藏  举报