1. 程式人生 > >PAT甲級1012,1015解題報告

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 C

M 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;
}