PAT甲级1012,1015解题报告

1012

1012 The Best Rank (25 分)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of CME and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of CM and E. Then there are Mlines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

题目大意:根据每个学生的C,M,E,A成绩(A为平均成绩,CME是输入的)每个成绩都有个排名。按照优先级A,C,M,E的顺序输出这个学生的最佳排名情况。即最佳排名数字和最佳成绩项目。

解题思路:刚开始没太注意,随便读入,然后把弄个函数指针,调用不同的排序方法。结果没通过。然后仔细看了题目,这个排名不是简单的排名,它是存在并列情况的。比如两者成绩是相同的,他们排名并列第一,但是下一位就不是第二了,而是第三了,这是比较坑的一点。还有就是N=0的情况,无论输入什么,都输出N/A,也是比较坑。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
using namespace std;
struct student{
	string id;
	int A, C, M, E;
	string resob;
	int resrank;
	int RC, RM, RE, RA;
};
bool cmpA(student a, student b) {
	return a.A > b.A;
}
bool cmpC(student a, student b) {
	return a.C > b.C;
}
bool cmpM(student a, student b) {
	return a.M > b.M;
}
bool cmpE(student a, student b) {
	return a.E > b.E;
}
int main()
{
	int N, M;
	cin >> N >> M;
	if (N == 0) {
		string cur;
		for (int i = 0; i < M; i++) {
			cin >> cur;
			cout << "N/A" << endl;
		}
	}
	else {
		vector<student> students;
		for (int i = 0; i < N; i++) {
			student cur;
			cin >>cur.id>> cur.C >> cur.M >> cur.E;
			cur.A = (cur.C + cur.M + cur.E) / 3;
			students.push_back(cur);
		}
		sort(students.begin(), students.end(), cmpA);
		int Arank = 1;
		students[0].resob = "A";
		students[0].resrank = 1;
		for (int i = 1; i < N; i++) {
			if (students[i].A < students[i - 1].A)
				Arank = i + 1;//处理并列
			students[i].resrank = Arank;
			students[i].resob = "A";
		}
		sort(students.begin(), students.end(), cmpC);
		int Crank = 1;
		if (students[0].resrank != 1) {
			students[0].resrank = 1;
			students[0].resob = "C";
		}
		for (int i = 1; i < N; i++) {
			if (students[i].C < students[i - 1].C)
				Crank = i + 1;//处理并列
			if (Crank < students[i].resrank) {
				students[i].resrank = Crank;
				students[i].resob = "C";
			}
		}
		sort(students.begin(), students.end(), cmpM);
		int Mrank = 1;
		if (students[0].resrank != 1) {
			students[0].resrank = 1;
			students[0].resob = "M";
		}
		for (int i = 1; i < N; i++) {
			if (students[i].M < students[i - 1].M)
				Mrank = i + 1;//处理并列
			if (Mrank < students[i].resrank) {
				students[i].resrank =Mrank;
				students[i].resob = "M";
			}
		}
		sort(students.begin(), students.end(), cmpE);
		int Erank = 1;
		if (students[0].resrank != 1) {
			students[0].resrank = 1;
			students[0].resob = "E";
		}
		for (int i = 1; i < N; i++) {
			if (students[i].E < students[i - 1].E)
				Erank = i + 1;//处理并列
			if (Erank < students[i].resrank) {
				students[i].resrank = Erank;
				students[i].resob = "E";
			}
		}
		
		for (int i = 0; i < M; i++) {
			string curid;
			cin >> curid;
			bool flag = false;
			for (int j = 0; j < N; j++) {
				if (students[j].id == curid) {
					cout << students[j].resrank << " " << students[j].resob << endl;
					flag = true;
					break;
				}
			}
			if (!flag)cout << "N/A" << endl;
		}
	}
	return 0;
}

1015 Reversible Primes (20 分)

reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.

Now given any two positive integers N (<10​5​​) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No

题目大意:给你一个数,一个进制,如果这个数和这个数在这个进制下的数反过来得到的数都是素数,那么输出yes,如果有一个不是,那么输出no。

解题思路:很水,判断一下是不是素数,然后进制转换一下,用定义得到进制下的数,不用考虑倒叙的情况了,定义的方法,余数就是倒着读进去的。所以没什么影响。然后把这个数在算回来就行了。算出来之后再判断是不是素数。

代码如下

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
using namespace std;
vector<int> getRadixnum(int num,int radix) {
	vector<int> res;
	while (num!=0) {
		res.push_back(num%radix);
		num = num / radix;
	}
	return res;
}
bool isPrime(int p) {
	bool flag = true;
	if (p <= 1)flag = false;
	else if (p == 2)flag = true;
	else {
		for (int i = 2; i < sqrt(p) + 1; i++) {
			if (p%i == 0) {
				flag = false;
				break;
			}
		}
	}
	return flag;
}
int getReverse(vector<int> tmp,int radix) {
	int sum = 0;
	for (int i = 0; i < tmp.size(); i++) {
		sum += (tmp[i] * pow(radix, tmp.size() - i - 1));
	}
	return sum;
}
int main()
{
	int n, d;
	while (cin >> n) {
		if (n <0)break;
		else if (n == 0) {
			cin >> d;
			cout << "No" << endl;
		}
		else {
			cin >> d;
			if (isPrime(n) && isPrime(getReverse(getRadixnum(n,d),d))) {
				cout << "Yes" << endl;
			}
			else {
				cout << "No" << endl;
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/TateBrwonJava/article/details/82944175