PKU C++课程期末编程题解答

总时间限制: 

1000ms

内存限制: 

65536kB

// 在此处补充你的代码

描述

近年来,国内电视剧吸引了越来越多的关注;有的以当红的演员阵容而吸引观众,比如《三生三世十里桃花》(Life After Life,Blooms Over Blooms);有的以贴近时代的剧情而备受关注,比如《人民的名义》(In the Name of People);有的则以精湛的演技赢得观众的喜欢,比如《大明王朝:1566》(Ming Dynasty: 1566)。
你的任务是根据电视剧的不同属性(演员、剧情和演技)对电视剧进行排行。

#include<iostream>
#include<cstring>
#include<list>
#include<algorithm>
using namespace std;

class TV_Drama{
	public:
	char name[100];
	int actor;
	int story;
	int acting_skill;
int main(){
	list<TV_Drama> lst;
	int n;
	
	cin>>n;
	char  _name[100];
	int _actor, _story, _acting_skill;
	for (int i=0; i<n; i++){
        cin.ignore();
        cin.getline(_name,100);
        cin>>_actor>>_story>>_acting_skill;
		lst.push_back(TV_Drama(_name, _actor, _story, _acting_skill));
	}

	lst.sort();
	for_each(lst.begin(), lst.end(), Printer);	
	cout<<endl;

	lst.sort(comparator_1);
	for_each(lst.begin(), lst.end(), Printer);	
	cout<<endl;

	lst.sort(comparator_2());
	for_each(lst.begin(), lst.end(), Printer);	
	cout<<endl;

	return 0;
}

输入

首先输入整数n,代表电视剧的个数。接下来,对于每个电视剧有两行输入:第一行一个字符串(可能含有空格,逗号,冒号等标点符号)作为电视剧的名字;第二行包括三个整数,分别为演员阵容、剧情和演技的评分。

输出

输出包括三行,分别为电视剧按演员阵容、剧情和演技的排行榜(评分由高到低),电视剧名字之间以分号隔开

样例输入

3
In the Name of People
98 97 99
Life After Life, Blooms Over Blooms
99 82 73
Ming Dynasty: 1566
97 100 100

样例输出

Life After Life, Blooms Over Blooms;In the Name of People;Ming Dynasty: 1566;
Ming Dynasty: 1566;In the Name of People;Life After Life, Blooms Over Blooms;
Ming Dynasty: 1566;In the Name of People;Life After Life, Blooms Over Blooms;

这道题目需要补全得有构造函数,回调Print函数, 三个重载的比较函数,其中第一个是在TV_Drama类中,第二个是使用回调函数重载,第三个是传入类对象重载

#include<iostream>
#include<cstring>
#include<list>
#include<algorithm>
using namespace std;

class TV_Drama {
public:
	char name[100];
	int actor;
	int story;
	int acting_skill;
	// 在此处补充你的代码
	TV_Drama(char _name[], int _actor, int _story, int _acting_skill) :
		actor(_actor), story(_story), acting_skill(_acting_skill) {
		strcpy(name, _name);
	}

	bool operator<(TV_Drama &x) {
		return actor > x.actor;
	}
};

void Printer(TV_Drama &x) {
	cout << x.name << ";";
}

bool comparator_1(TV_Drama &x, TV_Drama &y) {
	return x.story > y.story;
}

class comparator_2 {
public:
	comparator_2(){}
	bool operator()(TV_Drama &x1, TV_Drama &x2) {
		return x1.acting_skill > x2.acting_skill;
	}

};

	int main() {
		list<TV_Drama> lst;
		int n;

		cin >> n;
		char  _name[100];
		int _actor, _story, _acting_skill;
		for (int i = 0; i < n; i++) {
			cin.ignore();
			cin.getline(_name, 100);
			cin >> _actor >> _story >> _acting_skill;
			lst.push_back(TV_Drama(_name, _actor, _story, _acting_skill));
		}

		lst.sort();
		for_each(lst.begin(), lst.end(), Printer);
		cout << endl;

		lst.sort(comparator_1);
		for_each(lst.begin(), lst.end(), Printer);
		cout << endl;

		lst.sort(comparator_2());
		for_each(lst.begin(), lst.end(), Printer);
		cout << endl;

		return 0;
	}

总时间限制: 

1000ms

内存限制: 

1024kB

// 在此处补充你的代码

描述

给定一系列边长已知的矩形,输出对矩形进行两种排序的结果。

在第一种排序中,先按矩形的面积从大到小排序;若两个矩形的面积相同,则周长大的排在前。

在第二种排序中,先按矩形的周长从小到大排序;若两个矩形的周长相同,则面积小的排在前。

 

#include <iostream>
#include <set>
using namespace std;
int main() {
    multiset<Rectangle> m1;
    multiset<Rectangle, Comp> m2;
    int n, a, b;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        m1.insert(Rectangle(a, b));
        m2.insert(Rectangle(a, b));
    }
    for (multiset<Rectangle>::iterator it = m1.begin(); it != m1.end(); it++) {
        cout << *it << endl;
    }
    cout << endl;
    for (multiset<Rectangle>::iterator it = m2.begin(); it != m2.end(); it++) {
        cout << *it << endl;
    }
	return 0;
}

输入

第一行是一个整数n,表示输入的矩形个数。
接下来n行表示了n个矩形。每行有两个整数a与b,表示该矩形的长与宽。

输出

先用n行输出第一种排序的结果。每行两个整数,依次表示该矩形的面积与周长。
再输出一个空行。
最后用n行输出第二种排序的结果。每行两个整数,依次表示该矩形的面积与周长。

样例输入

6
3 8
4 6
10 2
6 6
4 8
3 6

样例输出

36 24
32 24
24 22
24 20
20 24
18 18

18 18
24 20
24 22
20 24
32 24
36 24

这道题目也是一道排序比较运算符的重载,注意技巧

#include <iostream>
#include <set>
using namespace std;
// 在此处补充你的代码
class Rectangle {
private:
	int width;
	int height;
public:
	Rectangle(int a, int b):width(a),height(b){}
	friend bool operator < (const Rectangle &Rect1,const Rectangle &Rect2) {
		if (Rect1.width*Rect1.height == Rect2.width * Rect2.height)
			return Rect1.width + Rect1.height > Rect2.width + Rect2.height;
		else
			return Rect1.width * Rect1.height > Rect2.width * Rect2.height;
	}  
	friend ostream& operator << (ostream &os, const Rectangle &Rect){
		os << Rect.height* Rect.width <<" "<< (Rect.height + Rect.width)*2;
		return os;
	}

	friend struct Comp;

};

struct Comp {
	bool operator() (const Rectangle &r1, const Rectangle &r2) {
		if (r1.height + r1.width == r2.height + r2.width)
			return r1.height*r1.width < r2.height * r2.width;
		else
			return r1.height + r1.width < r2.height + r2.width;
	}
};

int main() {
	multiset<Rectangle> m1;
	multiset<Rectangle, Comp> m2;
	int n, a, b;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a >> b;
		m1.insert(Rectangle(a, b));
		m2.insert(Rectangle(a, b));
	}

	for (multiset<Rectangle>::iterator it = m1.begin(); it != m1.end(); it++) {
		cout << *it << endl;
	}
	cout << endl;
    for (multiset<Rectangle>::iterator it = m2.begin(); it != m2.end(); it++) {
		cout << *it << endl;
	}
	return 0;
}

总时间限制: 

1000ms

内存限制: 

65536kB

// 在此处补充你的代码

描述

完成以下程序,使得输入的整数x,以及若干正整数,将
大于x的正整数输出;然后输入若干字符串,将字符串长度大于x的字符串输出

#include<iostream>
#include<algorithm>
#include<vector>
#include<bitset>

using namespace std;


class Printer{
int main(){

	int t;
	cin >> t;
	while(t--) {
		int n,x;
		cin>>x>>n;
		
		vector<int> intVec;
		for(int i = 0;i < n; ++i) {
			int y;
			cin >> y;
			intVec.push_back(y);
		}
		for_each(intVec.begin(), intVec.end(), Printer(x));
		cout<<endl;
		
		vector<string> strVec;
		for(int i = 0;i < n; ++i) {
			string str;
			cin >> str;
			strVec.push_back(str);
		}
		for_each(strVec.begin(), strVec.end(), Printer(x));
		cout<<endl;
	}
	return 0;
}

输入

第一行是整数t,表示一共t组数据
每组数据有三行 
第一行是整数x和整数 n 
第二行是n个整数 
第三行是n个不带空格的字符串

输出

对每组数据
先按原序输出第一行中大于x的正整数(数据保证会有输出) 
再按原序输出第二行中长度大于x的字符串 (数据保证会有输出)

样例输入

2
5 6
1 3 59 30 2 40
this is hello please me ha
1 1
4
this

样例输出

59,30,40,
please,
4,
this,

这个题目就是重写Printer函数,这里要特别注意传入一个构造函数

#include<iostream>
#include<algorithm>
#include<vector>
#include<bitset>

using namespace std;

class Printer {
	// 在此处补充你的代码
private:
	int x;
public:
	Printer(int x_):x(x_){}
	void operator()(int a) {
		if (a > x) cout << a << ",";
	}
	void operator()(string a) {
		if (a.size() > x)
			cout << a << ",";
	}
};
	int main() {
		int t;
		cin >> t;
		while (t--) {
			int n, x;
			cin >> x >> n;
			vector<int> intVec;
			for (int i = 0; i < n; ++i) {
				int y;
				cin >> y;
				intVec.push_back(y);
			}
			for_each(intVec.begin(), intVec.end(), Printer(x));
			cout << endl;

			vector<string> strVec;
			for (int i = 0; i < n; ++i) {
				string str;
				cin >> str;
				strVec.push_back(str);
			}
			for_each(strVec.begin(), strVec.end(), Printer(x));
			cout << endl;
		}
		return 0;
	}

总时间限制: 

1000ms

内存限制: 

65536kB

// 在此处补充你的代码

描述

输入n个整数,输出整数数列中大小排名前k的偶数

 

#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <string>
#include <map>
#include <set>

using namespace std;
class MyQueue
{
};
int main()
{
	int t;
	cin >> t;
	while(t--) {
		int n, k;
		cin >> n >> k;
		MyQueue q(k);
		for (int i = 0; i < n; ++i)
			cin >> q;
		cout<<q;
		cout << endl;
	}
	return 0; 
}

输入

有多组数据
第一行是数据组数 t
对每组数据: 
第一行为整数n (n>=3)和k
接下来的一行为n个整数,保证这些整数中至少有k个偶数。

输出

对每组数据,输出k个整数,降序排列,表示选出来的大小排名前k的偶数

样例输入

2
9 4
1 2 4 3 6 6 7 8 9
3 2
18 16 14

样例输出

8 6 6 4
18 16

这一道题目,是一道优先队列的题目

#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <string>
#include <map>
#include <set>

using namespace std;
class MyQueue
{
	// 在此处补充你的代码
public:
	int size;
	priority_queue<int> q;
	MyQueue(int k):size(k){}
    friend istream & operator>>(istream &is, MyQueue & queue) {
		int temp;
		is >> temp;
		if(temp%2==0)
			queue.q.push(temp);
		return is;
	}

	friend ostream & operator << (ostream &os, MyQueue & queue) {
		while(queue.size--) {
			os << queue.q.top() << " ";
			queue.q.pop();
		}
		return os;
	}
};
int main()
{
	int t;
	cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		MyQueue q(k);
		for (int i = 0; i < n; ++i)
			cin >> q;
		cout << q;
		cout << endl;
	}
	return 0;
}




总时间限制: 

1000ms

内存限制: 

65536kB

// 在此处补充你的代码

描述

输入x1 x2 x3 x4 x5 ,输出y = x5^5 + x4^4 + x3^3 + x2^2 + x1^1 + 1的y的值

#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
class MyFunc
{
};
int main()
{
	int n;
	cin >> n;
	while(n--) {
		vector<MyFunc> v;
		for (int i = 0; i < 5; ++i)
			v.push_back(MyFunc(i+1));
		int ans = 1;
		for (int i = 0; i < 5; ++i)
		{
			int m;
			cin >> m;
			ans += v[i](m);
		}
		cout << ans <<endl;
	}
}

输入

多组数据。第一行是数据组数 n
每组数据为一行,5个整数,x1 x2 x3 x4 x5。数值不大,不必考虑溢出

输出

对每组数据,输出一个整数y, y = x5^5 + x4^4 + x3^3 + x2^2 + x1^1 + 1

样例输入

2
2 2 2 2 2
1 1 1 1 1

样例输出

63
6

这道题目是一道简单的运算符重载问题

#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <map>
#include <set>

using namespace std;
class MyFunc
{
	// 在此处补充你的代码
private:
	int val;
public:
	MyFunc(int val_):val(val_){}
	int operator () (int n) {
		return pow(n, val);
	}
};
int main()
{
	int n;
	cin >> n;
	while (n--) {
		vector<MyFunc> v;
		for (int i = 0; i < 5; ++i)
			v.push_back(MyFunc(i + 1));
		int ans = 1;
		for (int i = 0; i < 5; ++i)
		{
			int m;
			cin >> m;
			ans += v[i](m);
		}
		cout << ans << endl;
	}
}

总时间限制: 

1000ms

内存限制: 

65536kB

// 在此处补充你的代码

描述

程序填空,使其按要求输出

#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <stack>
#include <iostream>
#include <set>
using namespace std;

int main() {
	int t;
	int  a[100];
	cin >> t;
	while(t--) {
		for(int i = 0;i < 12; ++i)
			cin >> a[i];
std::copy(b.begin(), b.end(), c);
		cout << endl;

	}
	return 0;
}

输入

第一行是个整数,表示输入数据组数 
每组数据一行,有12个整数

输出

对每组数据, 将12个整数从小到大排序并去除重复元素后输出

样例输入

2
34 5 4 6 3 9 8 34 5 3 3 18
31 2 4 6 2 9 8 31 5 3 3 18

样例输出

3 4 5 6 8 9 18 34 
2 3 4 5 6 8 9 18 31 

这道题目用集合去重,然后输出

#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <stack>
#include <iostream>
#include <set>
using namespace std;

int main() {
	int t;
	int  a[100];
	cin >> t;
	while (t--) {
		for (int i = 0; i < 12; ++i)
			cin >> a[i];
		// 在此处补充你的代码
		set<int> b(a,a+12);
		ostream_iterator<int> c(cout, " ");
		std::copy(b.begin(), b.end(), c);
		cout << endl;

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wwxy1995/article/details/90114593