PAT-Advenced Simulation
Abstract:模拟题意,恶心至极
1139 First Contact (30分)
解法一:我最爱的unordered_map
// 好难啊
1025 PAT Ranking (25分)
解法一:std::sort()
直接模拟题意,但是也挺麻烦的,大概实战只能想到这里了,总不可能自己排序吧
#include<iostream>
#include<vector>
#include<algorithm>
struct Rank {
std::string id;
int score, location, localrank;
};
int num, k, score;
std::string id;
std::vector<Rank> global_rank, local_rank;
int compare(Rank a, Rank b) {
return a.score == b.score ? a.id < b.id: a.score > b.score;
}
void parseInput() {
std::ios::sync_with_stdio(false); std::cin.tie(0);
std::cin >> num;
int index = 1;
for (int i = 0; i < num; i++) {
std::cin >> k;
for (int j = 0; j < k; j++) {
std::cin >> id >> score;
local_rank.push_back({ id,score,i + 1,-1 });
}
std::sort(local_rank.begin(), local_rank.end(), compare);
int rank = 1;
for (int j = 0; j < k; j++) {
if (j > 0 && local_rank[j].score != local_rank[j - 1].score)
rank = j + 1;
local_rank[j].localrank = rank;
global_rank.push_back(local_rank[j]);
}
local_rank.clear();
}
std::sort(global_rank.begin(), global_rank.end(), compare);
}
int main() {
parseInput();
int len = global_rank.size(), rank = 1;
std::cout << len << std::endl;
for (int i = 0; i < len; i++) {
if (i > 0 && global_rank[i].score != global_rank[i - 1].score)
rank = i + 1;
std::cout << global_rank[i].id << " "
<< rank << " "
<< global_rank[i].location << " "
<< global_rank[i].localrank << std::endl;
}
}
1028 List Sorting (25分)
解法一:std::sort()
标准套路题,能用sort无脑过
#include<iostream>
#include<vector>
#include<algorithm>
struct Student {
std::string id, name;
int score;
};
int num, c, age;
std::string id, name;
std::vector<Student> list;
int compare(Student a, Student b) {
if (c == 1) return a.id < b.id;
if (c == 2) return a.name <= b.name;
if (c == 3) return a.score <= b.score;
}
void parseInput() {
std::cin >> num >> c;
for (int i = 0; i < num; i++) {
std::cin >> id >> name >> age;
list.push_back({ id,name,age });
}
std::sort(list.begin(), list.end(), compare);
}
int main() {
std::ios::sync_with_stdio(false); std::cin.tie(0);
parseInput();
for (int i = 0; i < num - 1; i++) {
Student curr = list[i];
std::cout << curr.id << " "
<< curr.name << " "
<< curr.score << std::endl;
}
Student curr = list[num - 1];
std::cout << curr.id << " " << curr.name << " " << curr.score;
}
1012 The Best Rank (25分)
解法一(未通过)
差一个测试点死活过不了,裂开
#include<iostream>
#include<vector>
#include<algorithm>
struct mark {
int id = -1;
int score[4], best_rank = INT16_MAX;
char best_course;
} marks[1000000]; // 暴力写法,请勿模仿,请使用unordered_map替代
std::vector<mark*> mark_list;
std::vector<int> stu_list;
int mode;
const char courses[4] = { 'A','C','M','E' };
int compare(mark* a, mark* b) {
return a->score[mode] == b->score[mode] ?
a->id < b->id : a->score[mode] > b->score[mode];
}
void parseInput() {
std::ios::sync_with_stdio(false); std::cin.tie(0);
int num, tnum, id, c, m, e, a;
std::cin >> num >> tnum;
for (int i = 0; i < num; i++) {
std::cin >> id >> c >> m >> e;
a = (c + m + e) / 3.0 + 0.5 ;
marks[id] = { id,a,c,m,e };
mark_list.push_back(&marks[id]);
}
for (int i = 0; i < tnum; i++) {
std::cin >> id;
stu_list.push_back(id);
}
}
void genBestRank() {
for (mode = 0; mode < 4; mode++) {
std::sort(mark_list.begin(), mark_list.end(), compare);
mark_list[0]->best_rank = 1;
mark_list[0]->best_course = courses[mode];
for (int i = 1; i < mark_list.size(); i++) {
int rank = mark_list[i]->score[mode] == mark_list[i - 1]->score[mode] ?
mark_list[i - 1]->best_rank : i + 1;
if (rank < mark_list[i]->best_rank) {
mark_list[i]->best_rank = rank;
// std::cout << courses[mode];
mark_list[i]->best_course = courses[mode];
}
}
/*for (mark* i : mark_list)
std::cout << i->best_rank << " ";
std::cout << std::endl;*/
}
}
int main() {
parseInput();
genBestRank();
for (int i : stu_list) {
if (marks[i].id != -1)
std::cout << marks[i].best_rank
<< " " << marks[i].best_course << std::endl;
else
std::cout << "N/A" << std::endl;
}
}
1006 Sign In and Sign Out (25分)
解法一:std::sort()
标准套路题,sort无脑过
#include<iostream>
#include<vector>
#include<algorithm>
struct Person {
std::string id;
int ih, im, is;
int oh, om, os;
};
int num, mode = 0;
std::vector<Person> records;
std::string in, out;
int compare(Person a, Person b) {
if (mode == 0)
return a.ih == b.ih ? a.im == b.im ?
a.is < b.is : a.im < b.im : a.ih < b.ih;
else
return a.oh == b.oh ? a.om == b.om ?
a.os > b.os : a.om > b.om : a.oh > b.oh;
}
void parseInput() {
std::cin >> num;
records = std::vector<Person>(num);
for (int i = 0; i < num; i++) {
std::cin >> records[i].id;
scanf_s("%d:%d:%d", &records[i].ih, &records[i].im, &records[i].is);
scanf_s("%d:%d:%d", &records[i].oh, &records[i].om, &records[i].os);
}
for (mode = 0; mode < 2; mode++) {
std::sort(records.begin(), records.end(), compare);
if (mode == 0) in = records[0].id;
else out = records[0].id;
}
}
int main() {
parseInput();
std::cout << in << " " << out;
}