http://ac.jobdu.com/problem.php?pid=1203

题目描述:

输入一个ip地址串,判断是否合法。

输入:

输入的第一行包括一个整数n(1<=n<=500),代表下面会出现的IP地址的个数。
    接下来的n行每行有一个IP地址,IP地址的形式为a.b.c.d,其中a、b、c、d都是整数。

输出:

可能有多组测试数据,对于每组数据,如果IP地址合法则输出"Yes!”,否则输出"No!”。

样例输入:
2
255.255.255.255
512.12.2.3
样例输出:
Yes!
No!
提示:

合法的IP地址为:
a、b、c、d都是0-255的整数。

来源:
2006年华中科技大学计算机保研机试真题
解1:
根据.的位置直接判断,输入的格式是确定的。只要判断四个数字的正确性。
 #include<stdio.h>
#include<string.h> int main() {
int n;
while (scanf("%d", &n) != EOF) {
while (n--) {
char ip[];
scanf("%s", ip);
int len = strlen(ip);
int count = ;
int num = ;
for (int i = ; i < len; i++) {
if (ip[i] == '.') {
if (num >= && num <= ) {
count++;
}
num = ;
} else {
num = num * + ip[i] - '';
}
}
if (num >= && num <= ) {
count++;
}
if (count == ) {
printf("Yes!\n");
} else {
printf("No!\n");
}
}
}
return ;
}

解2:

C++中没有split(分割字符串)函数。因此可以构造一个。使用起来就比较方便。不过对于该题而言,这样略显复杂。

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector> using namespace std; char ip[]; void split(const string& s, const string& c, vector<string>& v)
{
string::size_type last, index;
index = s.find(c);
last = ;
while(string::npos != index)
{
v.push_back(s.substr(last, index-last)); last = index + c.size();
index = s.find(c, last);
}
if(last != s.length())
v.push_back(s.substr(last));
} bool isValid(char str[], vector<string> &ret)
{
int len=strlen(str);
if(len>) return false; string s=str;
string delim="."; split(s, delim, ret); int size=ret.size();
for(int i=; i<size; i++)
{
int num=atoi(ret[i].c_str());
if(num< || num>)
return false;
} return true;
} int main()
{
vector<string> ret;
int n;
while(scanf("%d", &n)!=EOF)
{
while(n--)
{
if(ret.empty()!=true)
ret.clear();
scanf("%s", ip);
bool isip=isValid(ip, ret);
if(isip)
printf("Yes!\n");
else
printf("No!\n");
}
} return ;
}

九度-题目1203:IP地址的更多相关文章

  1. 九度OJ 1203:IP地址 (字符串处理)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3038 解决:1496 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1<=n<=500) ...

  2. 九度 题目1421:Abor

    转载声明本文地址 http://blog.csdn.net/yangnanhai93/article/details/40563285 题目链接:http://ac.jobdu.com/problem ...

  3. 九度 题目1437:To Fill or Not to Fill

    题目描述: With highways available, driving a car from Hangzhou to any other city is easy. But since the ...

  4. 九度 题目1044:Pre-Post

    转载请注明本文链接http://blog.csdn.net/yangnanhai93/article/details/40658571 题目链接:pid=1044">http://ac ...

  5. 九度-题目1026:又一版 A+B

    http://ac.jobdu.com/problem.php?pid=1026 题目描述: 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m ...

  6. 九度-题目1195:最长&最短文本

    http://ac.jobdu.com/problem.php?pid=1195 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出 ...

  7. 九度 题目1154:Jungle Roads

    题目描写叙述: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid mon ...

  8. 九度 题目1454:Piggy-Bank 完全背包

    题目1454:Piggy-Bank 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1584 解决:742 题目描述: Before ACM can do anything, a budg ...

  9. 【九度OJ】题目1026:又一版 A+B 解题报告

    [九度OJ]题目1026:又一版 A+B 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1026 题目描述: 输入两个不超过 ...

随机推荐

  1. Enable CSS active pseudo styles in Mobile Safari

    http://alxgbsn.co.uk/2011/10/17/enable-css-active-pseudo-styles-in-mobile-safari/ document.addEventL ...

  2. # 20155327 2016-20017-3 《Java程序设计》第3周学习总结

    教材学习内容总结 第四章 认识对象 区分基本类型与类类型 基本类型: 1.整数:包括int,short,byte,long ,初始值为0 2.浮点型:float,double ,初始值为0.0 3.字 ...

  3. 1245 最小的N个和

    1245 最小的N个和 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond         题目描述 Description 有两个长度为 N 的序列 A 和 B, ...

  4. .NET core 项目部署在windows 服务器方法以及iis 访问报 500.19错误的解决办法

    将本地发布的服务本地运行没问题,发布上云windows 服务器就报 500.19 0x8007000d 是因为云服务器没有安装.net core相关的插件,比如.NET CORE sdk等,请按照该文 ...

  5. 手撕一个 Galgame 神器——Shub-Niggurath Project

    一.想法 Galgame 我们大概可以分为好用的 Galgame 和好玩的 Galgame,但是如果你把好玩的 Galgame 拿来用的话,有时候会十分让人着急.如果你躺在床上,一只手还在按压键盘实际 ...

  6. C#随堂

    顺序语句 上到下执行 分支语句 if    else switch() { case 1: Console.WriteLine(1); break; case 2: Console.WriteLine ...

  7. Python中的矩阵操作

    Numpy 通过观察Python的自有数据类型,我们可以发现Python原生并不提供多维数组的操作,那么为了处理矩阵,就需要使用第三方提供的相关的包. NumPy 是一个非常优秀的提供矩阵操作的包.N ...

  8. v-on 事件修饰符

    事件修饰符:   .stop 阻止冒泡 .prevent 阻止默认事件 .capture 添加事件侦听器时使用事件捕获模式 .self 只当该事件在该元素本身时(不是子元素)触发时才回调 .once ...

  9. DotNetOpenAuth Part 1 : Authorization 验证服务实现及关键源码解析

    DotNetOpenAuth 是 .Net 环境下OAuth 开源实现框架.基于此,可以方便的实现 OAuth 验证(Authorization)服务.资源(Resource)服务.针对 DotNet ...

  10. 作业 20181016-1 Alpha阶段贡献分配规则

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2244 条件:八位同学,总共80分贡献分(贡献分总数以实际为准),投票方式 ...