P7911 [CSP-J 2021] 网络连接
完全出错位置。。。
题目分析
需要注意的一些事情:
- 必须要有 \(3\) 个
.,要有 \(1\):。
给定一个串,为 a.b.c.d:e。
-
\(0\le a,b,c,d\le255,0\le e\le65535\)。
-
数必须要有 \(5\) 个数。
-
其他字母得爬。
大家总在说什么 \(STL\) 好用 (事实确实如此),但是在字符串的某些处理上 C 语言也有一些优秀之处,比如这道题上。
可以使用 strcmp,sprintf,sscanf。
这里简要介绍下下:
strcmp:
基本形式为 \(\rm strcmp(str1,str2)\)。
如果返回值小于 \(0\),则表示 \(\rm str1\) 小于 \(\rm str2\)。
如果返回值大于 \(0\),则表示 \(\rm str1\) 大于 \(\rm str2\)。
如果返回值等于 \(0\),则表示 \(\rm str1\) 等于 \(\rm str2\)。
sscanf:
这玩意儿与
scanf唯一的区别是它是以固定字符串为输入源的。格式为
sscanf(str,"...",...),表示从字符串 \(\rm str\) 读入。
sprintf:
这玩意儿与
printf唯一的区别是它是以固定字符串为输出源的。格式为
sprintf(str,"...",...),表示把""内的字符输到指定字符串 \(\rm str\) 中。
具体的使用可以自行查阅百度百科。
代码
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <map>
#include <string>
#include <cstring>
#define enter() putchar(10)
#define debug(c,que) cerr<<#c<<" = "<<c<<que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i=(st);i<=(ed);i++)cout<<arr[i]<<w;
namespace Newstd
{
inline int read()
{
char c;
bool flag=false;
while((c=getchar())<'0' || c>'9')
{
if(c=='-') flag=true;
}
int res=c-'0';
while((c=getchar())>='0' && c<='9')
{
res=(res<<3)+(res<<1)+c-'0';
}
return flag?-res:res;
}
inline void print(int x)
{
if(x<0)
{
putchar('-');x=-x;
}
if(x>9)
{
print(x/10);
}
putchar(x%10+'0');
}
}
using namespace Newstd;
using namespace std;
const int MA=105;
int n;
map<string,int>mp;
inline bool chk(int x)
{
if(x<0 || x>255)
{
return true;
}
return false;
}
inline bool chk(char* str)
{
int a=-1,b=-1,c=-1,d=-1,e=-1;
int t=sscanf(str,"%d.%d.%d.%d:%d",&a,&b,&c,&d,&e);
if(t!=5)
{
return false;
}
if(chk(a)==true || chk(b)==true || chk(c)==true || chk(d)==true)
{
return false;
}
if(e<0 || e>65535)
{
return false;
}
char tmp[MA];
sprintf(tmp,"%d.%d.%d.%d:%d",a,b,c,d,e);
return strcmp(str,tmp)==false;
}
int main(void)
{
//freopen("network.in","r",stdin);
//freopen("network.out","w",stdout);
std::ios::sync_with_stdio(false);
cin>>n;
for(register int i=1;i<=n;i++)
{
char opt[MA],name[MA];
cin>>opt>>name;
string now=name;
if(opt[0]=='S')
{
if(chk(name)==false)
{
cout<<"ERR"<<endl;
}
else if(mp.count(now)!=0)
{
cout<<"FAIL"<<endl;
}
else
{
cout<<"OK"<<endl;
mp[now]=i;
}
}
else
{
if(chk(name)==false)
{
cout<<"ERR"<<endl;
}
else if(mp.count(now)==0)
{
cout<<"FAIL"<<endl;
}
else
{
cout<<mp[now]<<endl;
}
}
}
return 0;
}

浙公网安备 33010602011771号