1095 解码PAT准考证

1095 解码PAT准考证_第1张图片
1095 解码PAT准考证_第2张图片
吐槽:
这道题完美诠释了一个真理:万物基于C语言!!!
最后一道题了,没想到陈姥姥会给个surprise!
以后刷题还是别用cin和cout了,是真的坑,这是一道,我记得1066 图像识别、1067 试密码这两道题也是不用scanf和printf过不去。

还有,虽然C++是支持C语言的,但是C语言是不支持C++的,所以在涉及字符串的时候务必不要用string类型,而是要用C风格的char数组,否则会造成大灾难!

题目很简单,直接上代码吧。

示例代码:

#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
class Stu {
public:
	string id;//准考证
	int grade;//成绩
};
bool cmp(Stu& s1, Stu& s2) {
	return s1.grade != s2.grade ? s1.grade > s2.grade:s1.id < s2.id;
}
int main() {
	int n, m; //cin >> n >> m;
	scanf("%d %d", &n, &m);
	vector<Stu> vec;
	for (int i = 0; i < n; i++) {
		char* temp = new char[64];
		int grade;
		scanf("%s %d", temp, &grade);
		Stu s;
		s.id = temp;
		s.grade = grade;
		vec.push_back(s);
	}
	for (int i = 0; i < m; i++) {//开始处理数据
		int temp; //cin >> temp;
		scanf("%d", &temp);
		bool flag = true;//查询结果是否为空  true表示为空
		if (temp == 1) {
			vector<Stu> v;
			char ch; scanf(" %c", &ch);
			for (int i = 0; i < vec.size(); i++) {
				if (vec[i].id[0] == ch) {
					v.push_back(vec[i]);
				}
			}
			printf("Case %d: 1 %c\n", i + 1, ch);
			sort(v.begin(), v.end(),cmp);
			for (int i = 0; i < v.size(); i++) {
			printf("%s %d\n", v[i].id.c_str(), v[i].grade);
				flag = false;
			}
		}
		else if (temp == 2) {
			int sum = 0,count = 0;
			char* str = new char[64];
			scanf("%s", str);
			for (int i = 0; i < vec.size(); i++) {
				if (vec[i].id.substr(1, 3) == str) {
					count++;
					sum += vec[i].grade;
					flag = false;
				}
			}
			printf("Case %d: 2 %s\n", i + 1, str);
			if (count > 0) printf("%d %d\n", count, sum);//cout << count << " " << sum << endl;
		}
		else if (temp == 3) {
			char* str = new char[64];
			scanf("%s", str);
			map<string, int> mapp;
			vector<Stu> v;//将就着用 把考场当成准考证  考生个数当成成绩
			for (int i = 0; i < vec.size(); i++) {
				if (vec[i].id.substr(4, 6) == str) {
					mapp[vec[i].id.substr(1,3)]++;
				}
			}
			for (map<string, int>::iterator it = mapp.begin(); it != mapp.end(); it++) {
				Stu s;
				s.grade = (*it).second;
				s.id = (*it).first;
				v.push_back(s);
				flag = false;
			}
			sort(v.begin(), v.end(),cmp);
			printf("Case %d: 3 %s\n", i + 1, str);
			for (int i = 0; i < v.size(); i++) {
				printf("%s %d\n", v[i].id.c_str(), v[i].grade);
			}

		}
		else {
			string str;
			getline(cin, str);
			printf("Case %d: 3 %s\n", i + 1, str.c_str());
			printf("NA\n");
			flag = false;
			continue;
		}
		if(flag) printf("NA\n");//cout << "NA" << endl;
	}
	return 0;
}

你可能感兴趣的:(PAT甲级题目)