|
发表于 2023-7-29 18:17:46
|
显示全部楼层
确实。晒一下我的代码 - #include<bits/stdc++.h>
- using namespace std;
- int n;
- map<string,int> server;
- bool check(string ip)
- {
- int t[6]={0,-1,-1,-1,-1,-1};
- int cnt = sscanf(ip.c_str(),"%d.%d.%d.%d:%d",&t[1],&t[2],&t[3],&t[4],&t[5]);
- if(cnt != 5) return false;
- for(int i=1;i<=4;i++)
- {
- if(t[i]<0 || t[i]>255) return false;
- }
- if(t[5]<0 || t[5]>65535) return false;
- char now[30];
- sprintf(now,"%d.%d.%d.%d:%d",t[1],t[2],t[3],t[4],t[5]);
- if(string(now) != ip)
- return false;
- return true;
- }
- int main()
- {
- cin>>n;
- string s,t;
- for(int i=1;i<=n;i++)
- {
- cin>>s>>t;
- if(!check(t))//检查ip地址是否合法
- {
- cout<<"ERR"<<endl;
- continue;
- }
- if(s=="Server"){
- if(server[t]==0)
- {
- server[t]=i;
- cout<<"OK"<<endl;
- }
- else
- cout<<"FAIL"<<endl;
- }
- else
- {
- if(server[t]>0)
- cout<<server[t]<<endl;
- else
- cout<<"FAIL"<<endl;
- }
- }
- return 0;
- }
复制代码 感觉差不多应该是这样 |
|