P7911 [CSP-J 2021] 网络连接
属于是大模拟中非常良心的一道了()
这一题的主要难点在于提取 a,b,c,d,e 并判断前导 0 和逗号,分号。
首先要介绍一个神器:sscanf()
. 以本题为例子,假设我们用一个字符数组 newp 存了一个地址串,想要从中提取 a,b,c,d,e,我们可以这样写:
int num = sscanf(place, "%lld.%lld.%lld.%lld:%lld", &a, &b, &c, &d, &e);`
其中,num 表示从前往后成功读取到的字符数量。若 num≠5,那么 place 显然不是一个合法的地址串,然后再判断 a,b,c,d,e 的大小即可。
有 sscanf()
当然有 sprintf()
,在本题中,我们可以以 a,b,c,d,e 重新以标准格式构造一个地址串,输出到一个字符串中。若这个新地址串与原地址串完全相同,说明原地址串合法,否则不合法。
另外有几个小坑点:
- 输出的编号是所有机器的编号而非服务机的序号
- a,b,c,d,e 如果用
%d
读取会炸 - 错误是输出
ERR
而非ERROR
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int N = 1010;
int n;
string server[N];
bool check(string s) {
ll a = 0, b = 0, c = 0, d = 0, e = 0;
const char* place; char newp[110];
place = s.c_str();
int num = sscanf(place, "%lld.%lld.%lld.%lld:%lld", &a, &b, &c, &d, &e);
if (num != 5) return 0;
sprintf(newp, "%lld.%lld.%lld.%lld:%lld", a, b, c, d, e);
string ss = newp;
if (ss != s) return 0;
if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 ||
d < 0 || d > 255 || e < 0 || e > 65535) return 0;
return 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
string op, place;
for (int i = 1; i <= n; ++i) {
cin >> op >> place;
if (!check(place)) {
cout << "ERR" << endl;
continue;
}
if (op == "Server") {
bool flag = 0;
for (int j = 1; j <= i; ++j) {
if (place == server[j]) {
flag = 1;
cout << "FAIL" << endl;
break;
}
}
if (!flag) {
server[i] = place;
cout << "OK" << endl;
}
}
else {
bool flag = 0;
for (int j = 1; j <= i; ++j) {
if (place == server[j]) {
flag = 1;
cout << j << endl;
break;
}
}
if (!flag) cout << "FAIL" << endl;
}
}
system("pause");
return 0;
}
UVA12412 A Typical Homework (a.k.a 师兄帮帮忙)
所有操作都可以暴力完成。
开一个结构体 Student
,存储 sid,uid,name,四科成绩及总分。用一个 vector
数组 s 来存储所有学生。另外还可以开一个 unordered_map
来存储所有学生的 sid.
对于添加操作,先判断新学生的 sid 是否重复,如果不是就添加到 s 中。
对于删除操作,遍历 s 数组,若 si.sid 或 si.name 与输入的字符串相匹配,则用 erase
函数将其删去。(这里有一个坑点,详见代码)
对于查询操作,几乎和删除操作一样,遍历 s 数组,若 si.sid 或 si.name 与输入的字符串相匹配,则计算其各项信息(详见代码)。
对于排名查询操作,这个系统良心地帮我们省去了()
对于班级成绩查询操作,基本和查询操作相同。
一些重要的坑点:
- 洛谷上的输出样例少了一些换行,以 PDF 上的为准
- PDF 上的
Showing the ranklist hurts students' self-esteem. Don't do that.
一句中的单引号是中文的,需要修改 - 遇到除数为 0 的情况,输出
-nan
而非nan
- 班级查询中的 Overall 后有冒号,而四科后是没有冒号的;Overall 查询的分别是全部通过的,通过 ≥3,≥2,≥1 科的和全部挂科的。
还有一些坑点,详见代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <unordered_map>
using namespace std;
const double eps = 1e-5;
struct Student {
string sid; // student ID
int cid; // class ID
string name;
int score[5]; // Chinese, Math, English and Programming
int total = 0;
} ;
vector<Student> s;
unordered_map<string, bool> sid;
void speedup() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
}
void print(int i) {
cout << s[i].sid << " " << s[i].cid << " ";
cout << s[i].name << " ";
for (int j = 1; j <= 4; ++j) cout << s[i].score[j] << " ";
cout << s[i].total << " " << fixed << setprecision(2) << s[i].total/4.0+eps << '\n';
}
void Main_Menu() { // Main Menu
cout << "Welcome to Student Performance Management System (SPMS).\n";
cout << '\n';
cout << "1 - Add\n";
cout << "2 - Remove\n";
cout << "3 - Query\n";
cout << "4 - Show ranking\n";
cout << "5 - Show Statistics\n";
cout << "0 - Exit\n";
cout << '\n';
}
void Add() {
cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish.\n";
string ss;
while (cin >> ss) {
if (ss == "0") break;
Student tmp;
tmp.sid = ss;
cin >> tmp.cid >> tmp.name;
for (int i = 1; i <= 4; ++i) {
cin >> tmp.score[i];
tmp.total += tmp.score[i];
}
if (!sid[ss]) s.push_back(tmp), sid[ss] = 1;
else cout << "Duplicated SID.\n";
//for (int i = 0; i < s.size(); ++i) print(i);
cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish.\n";
}
}
void Remove() {
cout << "Please enter SID or name. Enter 0 to finish.\n";
string ss;
while (cin >> ss) {
if (ss == "0") break;
int cnt = 0;
for (int i = 0; i < s.size(); ++i) {
Student a = s[i];
if (a.sid == ss || a.name == ss) {
sid[a.sid] = 0;
s.erase(s.begin()+i);
cnt ++;
i --;
}
}
//for (int i = 0; i < s.size(); ++i) print(i);
cout << cnt << " student(s) removed.\n";
cout << "Please enter SID or name. Enter 0 to finish.\n";
}
}
void Query() {
cout << "Please enter SID or name. Enter 0 to finish.\n";
string ss;
while (cin >> ss) {
if (ss == "0") break;
for (int i = 0; i < s.size(); ++i) {
if (s[i].sid == ss || s[i].name == ss) {
int rank = 1;
for (int j = 0; j < s.size(); ++j)
if (s[j].total > s[i].total) rank ++;
cout << rank << " " << s[i].sid << " " << s[i].cid << " ";
cout << s[i].name << " ";
for (int j = 1; j <= 4; ++j) cout << s[i].score[j] << " ";
cout << s[i].total << " " << fixed << setprecision(2) << s[i].total/4.0+eps << '\n';
}
}
cout << "Please enter SID or name. Enter 0 to finish.\n";
}
}
void Show_Ranklist() {
cout << "Showing the ranklist hurts students\' self-esteem. Don\'t do that.\n";
}
void Show_Statistics() {
cout << "Please enter class ID, 0 for the whole statistics.\n";
int cid;
cin >> cid;
int num = 0;
int sum[5] = {}, pass[5] = {}, fail[5] = {}, total[5] = {};
for (int i = 0; i < s.size(); ++i) {
if (s[i].cid == cid || cid == 0) {
int cnt = 0;
for (int j = 1; j <= 4; ++j) {
sum[j] += s[i].score[j];
if (s[i].score[j] >= 60) pass[j] ++, cnt ++;
else fail[j] ++;
}
num ++, total[cnt] ++;
}
}
for (int i = 1; i <= 4; ++i) {
if (i == 1) cout << "Chinese\n";
else if (i == 2) cout << "Mathematics\n";
else if (i == 3) cout << "English\n";
else cout << "Programming\n";
if (!num) cout << "Average Score: " << "-nan" << '\n';
else cout << "Average Score: " << fixed << setprecision(2) << (double)sum[i]/num+eps << '\n';
cout << "Number of passed students: " << pass[i] << '\n';
cout << "Number of failed students: " << fail[i] << '\n';
cout << '\n';
}
cout << "Overall:\n";
cout << "Number of students who passed all subjects: " << total[4] << '\n';
cout << "Number of students who passed 3 or more subjects: " << total[4]+total[3] << '\n';
cout << "Number of students who passed 2 or more subjects: " << total[4]+total[3]+total[2] << '\n';
cout << "Number of students who passed 1 or more subjects: " << total[4]+total[3]+total[2]+total[1] << '\n';
cout << "Number of students who failed all subjects: " << total[0] << '\n';
cout << '\n';
}
int main() {
// freopen("UVA12412.in", "r", stdin);
// freopen("UVA12412.out", "w", stdout);
speedup();
int op;
Main_Menu();
while (cin >> op) {
if (op == 0) break;
else if (op == 1) Add();
else if (op == 2) Remove();
else if (op == 3) Query();
else if (op == 4) Show_Ranklist();
else Show_Statistics();
Main_Menu();
}
return 0;
}
CF1280B Beingawesomeism
分类讨论即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> pii;
int t, n, m, ans;
char s[65][65];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> t;
while (t -- ) {
ans = 4;
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> s[i]+1;
bool isA = 0, allA = 1, pointA = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (s[i][j] == 'A') {
isA = 1;
if ((i == 1 && j == 1) || (i == 1 && j == m) ||
(i == n && j == 1) || (i == n && j == m)) pointA = 1;
}
else allA = 0;
}
}
if (!isA) cout << "MORTAL" << endl;
else if (allA) cout << 0 << endl;
else {
bool colA;
colA = 1;
for (int i = 1; i <= n; ++i) if (s[i][1] == 'P') colA = 0;
if (colA) {
cout << 1 << endl;
continue;
}
colA = 1;
for (int i = 1; i <= n; ++i) if (s[i][m] == 'P') colA = 0;
if (colA) {
cout << 1 << endl;
continue;
}
colA = 1;
for (int j = 1; j <= m; ++j) if (s[1][j] == 'P') colA = 0;
if (colA) {
cout << 1 << endl;
continue;
}
colA = 1;
for (int j = 1; j <= m; ++j) if (s[n][j] == 'P') colA = 0;
if (colA) {
cout << 1 << endl;
continue;
}
if (pointA) {cout << 2 << endl; continue;}
for (int i = 1; i <= n; ++i) {
colA = 1;
for (int j = 1; j <= m; ++j) {
if (s[i][j] == 'P')
colA = 0;
}
if (colA) {
cout << 2 << endl;
break;
}
}
if (colA) continue;
for (int j = 1; j <= m; ++j) {
colA = 1;
for (int i = 1; i <= n; ++i) {
if (s[i][j] == 'P')
colA = 0;
}
if (colA) {
cout << 2 << endl;
break;
}
}
if (colA) continue;
colA = 0;
for (int i = 1; i <= n; ++i) if (s[i][1] == 'A') colA = 1;
if (colA) {
cout << 3 << endl;
continue;
}
colA = 0;
for (int i = 1; i <= n; ++i) if (s[i][m] == 'A') colA = 1;
if (colA) {
cout << 3 << endl;
continue;
}
colA = 0;
for (int j = 1; j <= m; ++j) if (s[1][j] == 'A') colA = 1;
if (colA) {
cout << 3 << endl;
continue;
}
colA = 0;
for (int j = 1; j <= m; ++j) if (s[n][j] == 'A') colA = 1;
if (colA) {
cout << 3 << endl;
continue;
}
cout << 4 << endl;
}
}
system("pause");
return 0;
}