1. 程式人生 > >PAT甲級1100,1101解題報告

PAT甲級1100,1101解題報告

1100 Mars Numbers (20 分)

People on Mars count their numbers with base 13:

  • Zero on Earth is called "tret" on Mars.
  • The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.
  • For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.

For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.

Output Specification:

For each number, print in a line the corresponding number in the other language.

Sample Input:

4
29
5
elo nov
tam

Sample Output:

hel mar
may
115
13

題目大意:給定火星文表示數字的規則,按照規則雙向轉換地球數字和火星數字。

解題思路:沒什麼好方法。。switch不支援string引數,就寫一堆if也能過,注意格式換行吃回車就行了。

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
int main()
{
	int N;
	scanf("%d", &N);
	getchar();
	for (int i = 0; i < N; i++) {
		string cur;
		getline(cin,cur);
		if (isdigit(cur[0])) {
			int x = atoi(cur.c_str());
			int k = x / 13;
			switch (k)
			{
			case 0:break;
			case 1:cout << "tam"; break;
			case 2:cout << "hel"; break;
			case 3:cout << "maa"; break;
			case 4:cout << "huh"; break;
			case 5:cout << "tou"; break;
			case 6:cout << "kes"; break;
			case 7:cout << "hei"; break;
			case 8:cout << "elo"; break;
			case 9:cout << "syy"; break;
			case 10:cout << "lok"; break;
			case 11:cout << "mer"; break;
			case 12:cout << "jou"; break;
			default:
				break;
			}
			int t = x % 13;
			if (t == 0 && k == 0) {
				cout << "tret" << endl;
			}
			else if (t == 0)
				cout << endl;
			else {
				if (k != 0)
					cout << " ";
				switch (t)
				{
				case 0:break;
				case 1:cout << "jan" << endl; break;
				case 2:cout << "feb" << endl; break;
				case 3:cout << "mar" << endl; break;
				case 4:cout << "apr" << endl; break;
				case 5:cout << "may" << endl; break;
				case 6:cout << "jun" << endl; break;
				case 7:cout << "jly" << endl; break;
				case 8:cout << "aug" << endl; break;
				case 9:cout << "sep" << endl; break;
				case 10:cout << "oct" << endl; break;
				case 11:cout << "nov" << endl; break;
				case 12:cout << "dec" << endl; break;
				default:
					break;
				}
			}
		}
		else {
			int index = -1;
			for (int i = 0; i < cur.size(); i++) {
				if (cur[i] == ' ') {
					index = i;
					break;
				}
			}
			if (index == -1) {
				if (cur == "tret") {
					cout << 0 << endl;
				}
				if (cur == "jan") {
					cout << 1 << endl;
				}
				if (cur == "feb") {
					cout << 2 << endl;
				}
				if (cur == "mar") {
					cout << 3 << endl;
				}
				if (cur == "apr") {
					cout << 4 << endl;
				}
				if (cur == "may") {
					cout << 5 << endl;
				}
				if (cur == "jun") {
					cout << 6 << endl;
				}
				if (cur == "jly") {
					cout << 7 << endl;
				}
				if (cur == "aug") {
					cout << 8 << endl;
				}
				if (cur == "sep") {
					cout << 9 << endl;
				}
				if (cur == "oct") {
					cout << 10 << endl;
				}
				if (cur == "nov") {
					cout << 11 << endl;
				}
				if (cur == "dec") {
					cout << 12 << endl;
				}
				if (cur == "tam") {
					cout << 13 << endl;
				}
				if (cur == "hel") {
					cout << 26 << endl;
				}
				if (cur == "maa") {
					cout << 39 << endl;
				}
				if (cur == "huh") {
					cout << 52 << endl;
				}
				if (cur == "tou") {
					cout << 65 << endl;
				}
				if (cur == "kes") {
					cout << 78 << endl;
				}
				if (cur == "hei") {
					cout << 91 << endl;
				}
				if (cur == "elo") {
					cout << 104 << endl;
				}
				if (cur == "syy") {
					cout << 117 << endl;
				}
				if (cur == "lok") {
					cout << 130 << endl;
				}
				if (cur == "mer") {
					cout << 143 << endl;
				}
				if (cur == "jou") {
					cout << 156 << endl;
				}

			}
			else {
				string a = cur.substr(0, index);
				string b = cur.substr(index + 1);
				int sum = 0;
				if (a == "tam") {
					sum += 13;
				}
				if (a == "hel") {
					sum += 26;
				}
				if (a == "maa") {
					sum += 39;
				}
				if (a == "huh") {
					sum += 52;
				}
				if (a == "tou") {
					sum += 65;
				}
				if (a == "kes") {
					sum += 78;
				}
				if (a == "hei") {
					sum += 91;
				}
				if (a == "elo") {
					sum += 104;
				}
				if (a == "syy") {
					sum += 117;
				}
				if (a == "lok") {
					sum += 130;
				}
				if (a == "mer") {
					sum += 143;
				}
				if (a == "jou") {
					sum += 156;
				}
				if (b == "jan") {
					sum += 1;
				}
				if (b == "feb") {
					sum += 2;
				}
				if (b == "mar") {
					sum += 3;
				}
				if (b == "apr") {
					sum += 4;
				}
				if (b == "may") {
					sum += 5;
				}
				if (b == "jun") {
					sum += 6;
				}
				if (b == "jly") {
					sum += 7;
				}
				if (b == "aug") {
					sum += 8;
				}
				if (b == "sep") {
					sum += 9;
				}
				if (b == "oct") {
					sum += 10;
				}
				if (b == "nov") {
					sum += 11;
				}
				if (b == "dec") {
					sum += 12;
				}
				cout << sum << endl;
			}
		}
	}
	return 0;
}

1101 Quick Sort (25 分)

There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Given N distinct positive integers after a run of partition, could you tell how many elements could be the selected pivot for this partition?

For example, given N=5 and the numbers 1, 3, 2, 4, and 5. We have:

  • 1 could be the pivot since there is no element to its left and all the elements to its right are larger than it;
  • 3 must not be the pivot since although all the elements to its left are smaller, the number 2 to its right is less than it as well;
  • 2 must not be the pivot since although all the elements to its right are larger, the number 3 to its left is larger than it as well;
  • and for the similar reason, 4 and 5 could also be the pivot.

Hence in total there are 3 pivot candidates.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​). Then the next line contains N distinct positive integers no larger than 10​9​​. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output in the first line the number of pivot candidates. Then in the next line print these candidates in increasing order. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

Sample Input:

5
1 3 2 4 5

Sample Output:

3
1 4 5

題目大意:給一個數組,找出能夠做快排基準的數,即比左邊都大,比右邊都小這樣的數。

解題思路:不想搞花裡胡哨的,只想在數組裡解決,於是開了兩個map,存了一下從左到右遍歷過去到當前數為止的最大值,和從右到左遍歷過去到當前數為止的最小值,如果這兩個值和當前數都相等,那麼這個就可以作為基準了,利用set的特性,將每個這樣的數存入set輸出就是有序了。有一個坑是,如果沒有數吧,要輸出一個空行。

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
vector<int> a;
map<int, int> ma;
map<int, int> mb;
set<int> res;
int main()
{
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		int tmp;
		scanf("%d", &tmp);
		a.push_back(tmp);
	}
	int maxa = a[0];
	int minb = a[a.size() - 1];
	for (int i = 0; i < a.size(); i++) {
		if (a[i] > maxa) {
			maxa = a[i];
		}
		ma[i] = maxa;
	}
	for (int i = a.size() - 1; i > -1; i--) {
		if (a[i] < minb) {
			minb = a[i];
		}
		mb[i] = minb;
	}
	for (int i = 0; i < a.size(); i++) {
		if (ma[i] == a[i] && mb[i] == a[i]) {
			res.insert(a[i]);
		}
	}
	printf("%d\n", res.size());
	if (res.size() == 0)printf("\n");
	int q = 0;
	for (auto i = res.begin(); i != res.end(); i++) {
		if (q != res.size() - 1) {
			printf("%d ", *i);
			q++;
		}
		else
			printf("%d\n", *i);
	}
	return 0;


}