模拟代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n, p = 1, ans[1003]; // 没事干的ans数组
struct node{
string op, ad;
}a[1003];
int ws(int n) { // 位数
int sum = 0;
if (n == 0) return 1;
while (n) {
n /= 10;
sum++;
}
return sum;
}
bool check(string ad) {
int c1 = 0, c2 = 0;
for (int i = 0; i < ad.size(); i++) { // 统计 . 和 : 的数量
if (ad[i] == '.') c1++;
if (ad[i] == ':') c2++;
}
if (c1 != 3 || c2 != 1) return 0;
c1 = 0, c2 = 0;
for (int i = 0; i < ad.size(); i++) { // 检查顺序
if (ad[i] >= '0' && ad[i] <= '9') continue;
if (ad[i] == '.') c1++;
if (c1 == 3 && c2 == 0) break; // 当点有 3 个时,冒号肯定还有 1 个,为了避免后面判断错误,所以直接跳出循环
if ((c1 == 1 || c1 == 2 || c1 == 3) && c2 >= 1) return 0;
}
long long num = 0, nump = 1, st = -1, po = 0, cz = 0;
for (int i = 0; i < ad.size(); i++) {
if (ad[i] >= '0' && ad[i] <= '9') { // 数字
num = num * 10 + (ad[i] - '0');
if (!po) { // 记录起点
st = i;
po = 1;
}
cz = 1;
}
else if (nump >= 1 && nump <= 4) {
if (!cz) return 0;
if (ws(num) != i - st) return 0; // 位数不符合要求
if (!(num >= 0 && num <= 255)) return 0;
num = 0;
nump++;
po = 0;
}
}
if (nump != 5) return 0;
if (ws(num) != ad.size() - st) return 0;
if (!(num >= 0 && num <= 65535)) return 0;
return 1;
}
bool have_server(int n) {
for (int i = 1; i < n; i++) {
if (a[i].op == "Server" && a[i].ad == a[n].ad)
return 1;
}
return 0;
}
int find_server(string ad, int n) {
for (int i = 1; i < n; i++)
if (a[i].op == "Server" && a[i].ad == ad) return i;
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
cin >> a[i].op >> a[i].ad;
if (check(a[i].ad)) {
if (a[i].op == "Server") {
if (!have_server(i)) ans[i] = -1;
else ans[i] = -2;
}
if (a[i].op == "Client") {
int s = find_server(a[i].ad, i);
if (s) ans[i] = s;
else ans[i] = -2;
}
}
else ans[i] = -3;
}
for (int i = 1; i <= n; i++) {
if (ans[i] > 0) printf("%d\n", ans[i]);
else if (ans[i] == -1) printf("OK\n");
else if (ans[i] == -2) printf("FAIL\n");
else if (ans[i] == -3) printf("ERR\n");
}
return 0;
}
标签:return,ad,int,题解,P7911,else,++,ans,CSP
From: https://www.cnblogs.com/panda-lyl/p/18496122