PAT甲级1100,1101解题报告

49 篇文章 0 订阅
47 篇文章 0 订阅

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;


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值