关于PAT乙级一些题目要点的重新整理(二)

1028(题目及原解答https://blog.csdn.net/g28_gwf/article/details/80411679
1.注意若组数为0时的输出

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
struct Person{
	string name;
	int birthday;
};
bool cmp(Person a, Person b){
	return a.birthday < b.birthday;
}
int main(){
	int N;
	cin >> N;
	getchar();
	vector<Person> v;
	for(int i = 0; i < N; i++){
		string name;
		int y, m, d;
		cin >> name;
		scanf("%d/%d/%d", &y, &m, &d);
		int birthday = y * 10000 + m * 100 + d;
		if(birthday >= 18140906 && birthday <= 20140906){
			v.push_back(Person{name, birthday});
		}
	}
	sort(v.begin(), v.end(), cmp);
	if(v.size() != 0){
		cout << v.size() << ' ' << v[0].name << ' ' << v[v.size() - 1].name << endl;
	}else{
		cout << '0' << endl;
	}
	return 0;
}

1030(题目及原解答https://blog.csdn.net/g28_gwf/article/details/81212839
1.若要找一个有序数列中的最长序列,不应当默认由第一个元素作为起始
2.在确认一个最长序列ans后,下一轮检测从初始元素的ans后个元素开始
3.如果每次循环都进行判断会超时,因此当数列不符合要求时才break出循环,计算长度

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
	long N, p;
	cin >> N >> p;
	vector<int> v(N);
	for(int i = 0; i < N; i++){
		cin >> v[i];
	}
	sort(v.begin(), v.end());
	int ans = 0;
	for(int i = 0; i < N; i++){
		for(int j = i + ans; j < N; j++){
			if(v[i] * p < v[j]){
				break;
			}
			ans = j - i + 1 > ans ? j - i + 1 : ans;
		}
	}
	cout << ans << endl;
	return 0;
}

1033(题目及原解答https://blog.csdn.net/g28_gwf/article/details/81294970
1.注意第一行输入可能有空格(不知道为什么)
2.录入输入文字字符串后直接循环判断字符是否可输出

#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
	map<char, bool> m;
	string brokenKey;
	getline(cin, brokenKey);
	for(int i = 0; i < brokenKey.size(); i++){
		m[brokenKey[i]]++; 
		if(brokenKey[i] >= 'A' && brokenKey[i] <= 'Z'){
			m[brokenKey[i] + 32]++;
		}
	}
	string s;
	getline(cin, s);
	for(int i = 0; i < s.size(); i++){
		if(m['+'] == 1 && s[i] >= 'A' && s[i] <= 'Z'){
			continue;
		}else if(m[s[i]] == 1){
			continue;
		}else{
			cout << s[i];
		}
		
	}
	return 0;
}

1034(题目及原解答https://blog.csdn.net/g28_gwf/article/details/81461874
1.思路:四种运算作为四种函数,每种函数均调用格式化函数来输出三个分数。
2.格式化注意点:
分子分母符号问题;
分子分母是否相等;
分子分母约分;
带分数处理;

#include<iostream>
using namespace std;
long a, b, c, d;
void config(long a, long b) {
	if (a == 0) {
		cout << 0;
		return;
	}
	if (b == 0) {
		cout << "Inf";
		return;
	}
	bool isNeg = false;
	if (a < 0 || b < 0) {
		if (a < 0 && b < 0) {
			a = -a;
			b = -b;
			isNeg = false;
		}else if (a < 0) {
			a = -a;
			isNeg = true;
		}else if (b < 0){
			b = -b;
			isNeg = true;
		}
	}
	if (isNeg == true) {
		cout << "(-";
	}
	long A = a, B = b;
	while (B != 0) {
		long temp = A % B;
		A = B;
		B = temp;
	}
	a /= A;
	b /= A;
	if (a == b) {
		cout << '1';
	}else if (a > b) {
		if (a % b == 0) {
			cout << a / b;
		}else {
			cout << a / b << ' ' << a % b << '/' << b;
		}
	}else {
		cout << a << '/' << b;
	}
	if (isNeg == true) {
		cout << ')';
	}
}
void add() {
	config(a, b);
	cout << " + ";
	config(c, d);
	cout << " = ";
	config(a * d + c * b, b * d);
	cout << endl;
}
void sub() {
	config(a, b);
	cout << " - ";
	config(c, d);
	cout << " = ";
	config(a * d - c * b, b * d);
	cout << endl;
}
void mul() {
	config(a, b);
	cout << " * ";
	config(c, d);
	cout << " = ";
	config(a * c, b * d);
	cout << endl;
}
void divi() {
	config(a, b);
	cout << " / ";
	config(c, d);
	cout << " = ";
	config(a * d, c * b);
	cout << endl;
}
int main() {
	scanf("%ld/%ld %ld/%ld", &a, &b, &c, &d);
	add();
	sub();
	mul();
	divi();
	return 0;
}

1045(题目及原解答https://blog.csdn.net/g28_gwf/article/details/81448379
1.主要思想:排序,之后遍历排序前与排序后的数组,每次循环找出目前为止最大的数,如果某数排序前后位置不变且大于当前最大的数,则符合要求
2.注意当输出个数为零时需要输出一个换行符

#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int main(){
	int N;
	cin >> N;
	vector<int> v1(N);
	vector<int> v2(N);
	for(int i = 0; i < N; i++){
		cin >> v1[i];
		v2[i] = v1[i];
	}
	sort(v1.begin(), v1.end());
	int maxN = -1;
	set<int> s;
	for(int i = 0 ; i < N; i++){
		if(v1[i] == v2[i] && v2[i] > maxN){
			s.insert(v2[i]);
		}
		if(maxN < v2[i]){
			maxN = v2[i];
		}
	}
	cout << s.size() << endl;
	if(s.size() != 0){
		for(set<int>::iterator it = s.begin(); it != s.end(); it++){
			if(it != (--s.end())){
				cout << *it << ' ';
			} else{
				cout << *it << endl;
			}
		}	
	}else{
		cout << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/g28_gwf/article/details/82503866