PAT 2022春季乙级 C++ 满分题解

PAT 2022春季乙级 C++ 满分题解

7-1暴力破解(15分)

算法标签 排序 枚举

AC代码

#include
using namespace std;
const int N = 10;
int a[N];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=0;i>a[i];
    }
    sort(a,a+n);
    int cnt=0;
    for(int i=0;i

7-2 学霸 (20 分)

算法标签 结构体 排序 模拟

AC代码

#include 
#include 
#include 
using namespace std;
const int MAXN = 1e6;
struct node {
	int num;		//编号
	int period;		//学时
	int cnt;		//门数
}per[MAXN];
bool cmp(struct node a, struct node b){
	if(a.period != b.period)
		return a.period > b.period;
	else if (a.period == b.period && a.cnt != b.cnt)
		return a.cnt > b.cnt;
	else if (a.period == b.period && a.cnt == b.cnt)
		return a.num < b.num;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int N, peopleNum, period;
	cin >> N;
	for (int i = 0; i < N; i++){
		cin >> period >>peopleNum;
		for (int j = 0; j < peopleNum; j++) {
			int temp;
			cin >> temp;
			per[temp].num = temp;
			per[temp].cnt++;
			per[temp].period += period;
		}
	}
	sort(per, per + MAXN, cmp);
	cout << per[0].period << " " << per[0].cnt << endl;
	cout << setfill('0') << setw(5) << per[0].num;
	for (int i = 1; i < MAXN; i++) {
		if (per[i].cnt == per[0].cnt && per[i].period == per[0].period) {
			cout << " " << per[i].num;
		}
	}
	return 0;
}

7-3 排课 (20 分)

算法标签 结构体 模拟

AC代码

#include 
#define int long long
using namespace std;
const int MAXN = 1e4 + 7;
struct node {
	int L;
	set time_t;
	int time;
	int room;
	int cnt = 0;//本班级排课与老师不冲突
}per[MAXN];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int all = 0;
	int N, M, K;
	cin >> N >> M >> K;
	for (int i = 0; i < N; i++) {
		cin >> per[i].L;
		for (int j = 0; j < per[i].L; j++) {
			int temp;
			cin >> temp;
			per[i].time_t.insert(temp);
		}
		cin >> per[i].time >> per[i].room;
 
		//处理两个班之间的 跟所有的班级内排课不冲突的班比较
		if (i)
		{
			for (int m = 0; m < i; m++) {
				if (per[i].time == per[m].time && per[i].room == per[m].room && !per[m].cnt) {
					cout << "ERROR: Conflict between " << i + 1 << " and " << m + 1 << "." << endl;
					per[i].cnt = 1;
					all++;
				}
			}
		}
		//处理班内的
		if (per[i].time_t.find(per[i].time) != per[i].time_t.end())
		{
			cout << "ERROR: Conflict with instructor for " << i + 1 << "." << endl;
			per[i].cnt = 1;
			all++;
		}
	}
	if (!all) cout << "Perfect Arrangement for " << N << " classes!";
	return 0;
}

考场上认为比较复杂 没有模拟出来,下来仔细想想,本质为循环嵌套查找

7-4 简易测谎 (20 分)

算法标签 字符串 模拟 排序

AC代码

#include
#define int long long
using namespace std;
const int N = 105; 
string check(string s, int t){
    int cnt=0;
    // 条件1
    if(s[0]=='f'){
        cnt-=2;
    }
    // 条件2
    if(s[s.size()-1]=='a'){
        cnt-=1;
    }
    vector l(N, 0);
    vector ll(N, 0 );
    int len=1;
    // 条件3
    for(int i=0;i());
    sort(ll.begin(),ll.end(), greater());
    
    for(int i=0;i5){
            cnt+=3;
        }
        else{
            break;
        }
    }
    for(int i=0;i3){
            cnt+=5;
        }
        else{
            break;
        }
    }
    if(cnt>t){
        return to_string(cnt)+"!!!";
    }
    else{
        return to_string(cnt);
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,t,k;
    cin>>n>>t>>k;
    while(k--){
        string s;
        cin>>s;
        cout<

考场上认为比较复杂 没有模拟出来 下来理清思路,依次模拟

7-5 前K大数 (25 分)

算法标签 队列

思路

由于本题空间限制严格,直接排序导致内存超限,可以采用优先队列优化,优先队列只存储前5大的值即可。

AC代码

#include
#define int long long
using namespace std;
const int INF = -0x3f3f3f3f, K = 5;
int a[K] = {INF};
priority_queue, greater> q;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    // cin 容易STL
    // cin>>n>>k;
    scanf("%lld%lld", &n,&k);
    if(nK){
            q.pop();
        }
    }
    int p=0;
    while (!q.empty()){
		a[p++] = q.top();
		q.pop();
	}
	for (int i = p - 1, j = 0; j < k; i--, j++) {
		if (i == p - 1) {
		    printf("%d", a[i]);
		}
		else {
		    printf(" %d", a[i]);
		}
	}
    return 0;
}

考场上用sort,只得了13分
原创不易 转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈PAT 2022春季乙级 C++ 满分题解_第1张图片

你可能感兴趣的:(PAT乙级考试题解,算法,图论,数据结构)