题目下载以及满分代码
A.Prime Day
签到题,可以使用stoi将字符串转换为数字,然后试除法判断是否为素数。
#include<iostream>
#include<string>
using namespace std;
bool check(const string &s) {
int a = stoi(s);
//0 1不是素数
if (a <= 1)
return false;
for (int i = 2; i * i <= a; i++)
if (a % i == 0)
return false;
return true;
}
int main() {
string s;
bool op = true, op2 = false;;
cin >> s;
while (s.length()) {
if (op2)
cout << endl;
op2 = true;
cout << s << " ";
if (check(s))
cout << "Yes";
else {
cout << "No";
op = false;
}
//取子串
s = s.substr(1);
}
if (op)
cout << endl << "All Prime!";
return 0;
}
B.The Judger
使用unordered_set来做,比set效率高,如果不要求集合有序的话。
#include<iostream>
#include<unordered_set>
#include<vector>
#define ac cin.tie(0);cin.sync_with_stdio(0);
using namespace std;
unordered_set<int> set1;
int arr[20][1010];
bool vis[20];
int n, m;
//是否为集合元素的差
bool check(int a) {
unordered_set<int>::iterator it = set1.begin();
for (; it != set1.end(); it++) {
if (set1.find((*it) + a) != set1.end())
return true;
}
return false;
}
int main() {
ac
int a, b;
cin >> a >> b;
set1.insert(a);
set1.insert(b);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> arr[i][j];
//总共m轮
for (int i = 1; i <= m; i++) {
vector<int> tmp;
for (int j = 1; j <= n; j++) {
//已经被淘汰的人不需要考虑
if (vis[j])
continue;
//存在集合中或者不是集合的差,本轮被淘汰
if (set1.count(arr[j][i]) || !check(arr[j][i]))
tmp.push_back(j);
else
//加入到集合中
set1.insert(arr[j][i]);
}
//本轮有人被淘汰
if (tmp.size()) {
//Round #5: 3 is out.
for (int j:tmp) {
cout << "Round #" << i << ": " << j << " " << "is out." << endl;
vis[j] = true;
}
}
}
vector<int> ans;
//求最后的胜者
for (int i = 1; i <= n; i++)
if (!vis[i])
ans.push_back(i);
if (ans.size()) {
cout << "Winner(s):";
for (int i:ans)
cout << " " << i;
} else
cout << "No winner.";
return 0;
}
C.Safari Park
简单的图论题,细心即可/
#include<iostream>
#include<cstring>
#define ac cin.tie(0);cin.sync_with_stdio(0);
using namespace std;
const int MAXN = 510;
bool G[MAXN][MAXN], vis[MAXN];
int arr[MAXN];
int N, R, K, M;
int main() {
ac
int a, b;
cin >> N >> R >> K;
while (R--) {
cin >> a >> b;
G[a][b] = G[b][a] = true;
}
cin >> M;
while (M--) {
//记录动物数
int cnt = 0;
//方案是否可行
bool op = true;
memset(vis, false, sizeof(vis));
for (int i = 1; i <= N; i++)
cin >> arr[i];
for (int i = 1; i <= N; i++) {
if (!vis[arr[i]]) {
++cnt;
vis[arr[i]] = true;
}
for (int j = i + 1; j <= N; j++) {
//相邻且动物一样
if (G[i][j] && arr[i] == arr[j])
op = false;
}
}
if (cnt < K)
cout << "Error: Too few species.";
else if (cnt > K)
cout << "Error: Too many species.";
else {
if (op)
cout << "Yes";
else
cout << "No";
}
if (M)
cout << endl;
}
return 0;
}
D.Replacement Selection
模拟题,需要很细心,使用优先队列解决,注意把题意理解了,样例手算一遍。
#include<iostream>
#include<queue>
#include<vector>
#define ac cin.tie(0);cin.sync_with_stdio(0);
using namespace std;
const int MAXN = 100010;
struct node {
int val;
bool op;
node() : op(false) {
};
//优先排op为false的,也就是本次run的,run相同则按照大小顺序排序
bool operator<(const node &b) const {
if (op != b.op)
return op > b.op;
else
return val > b.val;
}
} arr[MAXN];
int N, M;
priority_queue<node> Q;
int main() {
ac
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> arr[i].val;
arr[i].op = false;
}
int pos = 0;
//前M个元素入队
for (int i = 0; i < M; i++)
Q.push(arr[pos++]);
while (true) {
//记录本次run的答案
vector<int> v;
while (!Q.empty()) {
node tmp = Q.top();
//队列全都是下一次run,则break
if (tmp.op)
break;
Q.pop();
v.push_back(tmp.val);
//后面没有需要排序的数字了
if (pos == N)
continue;
//比出队的数字小,那么op为true代表下一次run
if (arr[pos].val < tmp.val)
arr[pos].op = true;
Q.push(arr[pos++]);
}
//输出结果
for (int i = 0; i < v.size(); i++) {
if (i)
cout << " ";
cout << v[i];
}
if (Q.empty())
break;
vector<node> pre;
while (!Q.empty()) {
pre.push_back(Q.top());
Q.pop();
}
//本轮run结束了,进行下一轮run,原本下一轮run的元素将op重新置为false,开始新一轮排序,
for (node &a:pre) {
a.op = false;
Q.push(a);
}
cout << endl;
}//while
return 0;
}
PAT冲冲冲!!!