样例4能过肯定ac,格式判断我写了两天。
查看原帖
样例4能过肯定ac,格式判断我写了两天。
362867
jiangyf70楼主2021/10/27 15:27
#include<bits/stdc++.h>
using namespace std;

string s[10005];
int n;


bool checkgs(string p)  //格式判断
{
    int a[10];
    for(int i = 0; i < 6; i++) a[i] = -1;
    sscanf(p.c_str(), "%d.%d.%d.%d:%d", &a[0], &a[1],&a[2],&a[3],&a[4]); //p.c_str()将p转为字符数组就可以使用sscanf了。

    for(int i = 0; i < 4; i++)
    {
        if(a[i] < 0 || a[i] > 255) return 0;  //没有获取到的数是-1,也是非法值
    }
    if(a[4] < 0 || a[4] > 65535) return 0;
    string s = to_string(a[0]) + '.' + to_string(a[1]) + '.' + to_string(a[2]) + '.' + to_string(a[3]) + ':' + to_string(a[4]) ;
    if(s != p)  return 0; //用数字拼出来的字符串如果和原来的不相同,说明格式不正确
    return 1;
}


int checkcf(string p) //重复判断
{
    for(int i = 1; i <= n; i++)
    {
        if(s[i] == p) return i; //返回数组下标
    }
    return -1;
}

int main()
{

    //freopen("network4.in", "r", stdin);
    //freopen("network4.out", "w", stdout);

    cin >> n;

    string a, p;
    for(int i = 1; i <= n; i++)
    {
        cin >> a;
        if(a == "Server")
        {
            cin >> p;
            if(checkgs(p))
            {
                if(checkcf(p) == -1)
                {
                    s[i] = p;  //将server的ip 存入 数组下标i的位置,
                    cout << "OK" << endl;
                }
                else cout << "FAIL" << endl;
            }
            else cout << "ERR" << endl;

        }else if (a == "Client")
        {
            cin >> p;
            if(checkgs(p) == 1)
            {
                if(checkcf(p) != -1 ) cout << checkcf(p) << endl;  //输出server ip所在的下标
                else cout << "FAIL" << endl;

            }
            else cout << "ERR" << endl;
        }

    }

    return 0;

}

2021/10/27 15:27
加载中...