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 1818 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;elsereturn 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;elsereturn 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;
}
PKU C++课程期末编程题解答相关推荐
- 京东校招java面试题_京东2018校招编程题解答(Java)
写在前面 本文主要是解答这次校招中京东的笔试编程题,这次京东的笔试编程题比较难,涉及KMP算法.manacher算法等.文中的解法也是在观看了左神(左程云)9月20号在牛客网的直播后,自己花时间写出来 ...
- python期末编程题_Python 语言程序设计二级教程第七章编程题
编程题1 f=open('file1.txt','w') f.write('My name is Lin') f.write('I am from China') f.close() k=open(' ...
- C Primer Plus 第02章 C语言概述 学习笔记及复习题、编程题解答
第二章 C语言概述 1. 解析第一个C程序 一个基本的程序结构包含如下部分: #include<stdio.h> // 包含另一个文件,C编译器软件包的标准部分,提供键盘输入和屏幕输出的支 ...
- 网易2019实习生招聘编程题解答
问题一: 牛牛找工作 为了找到自己满意的工作,牛牛收集了每种工作的难度和报酬.牛牛选工作的标准是在难度不超过自身能力值的情况下,牛牛选择报酬最高的工作.在牛牛选定了自己的工作后,牛牛的小伙伴们来找牛牛 ...
- 网易云课堂 计算机入门 期末 编程题
你的程序要读入一篇英文文章,然后统计其中的单词数来输出.需要统计的数据为: 总的单词数量: 含有1个字母到10个字母的单词的数量. 单词和单词的间隔是由以下标点符号形成的:空格.tab.回车换行.逗号 ...
- C语言期末编程题题库
目录 第一章 7-1 实验1-计算梯形的面积 7-2 实验1-求二元一次方程的解 7-1 Hello World! 7-2 圆的面积 7-3 长方形的周长 第二章 7-1 实验二1-计算摄氏温度 7- ...
- 复习笔记:数据库编程题
编程题(共10分) 有一个"学生选课成绩系统"数据库,数据库中包括三个表: (1)"学生"表Student由学号(Sno).姓名(Sname).性别(Ssex) ...
- Python课程期末考试编程题自动批卷原理与实现模板
适用场合: 1)Python程序设计课程上机或实验作业自动批阅. 2)Python程序设计课程期末考试编程题的自动评分. 设计思路: 1)编写考试试卷程序文件,定义好每个试题的函数接口和预期功能,详细 ...
- java程序设计试题_《Java语言程序设计》期末考试模拟试题——填空题和编程题...
一.根据题意,填写出空格中的内容 Java平台包括三个技术方向,其中J2ME代表____________.J2SE代表___________.J2EE代表____________.2.面向对象的四大概 ...
最新文章
- 彩色空间及cvtColor解析
- 中国工程院谭建荣:人工智能应用得再好,最核心的算法不行,创新能力就不行丨MEET2021...
- qq飞车登陆服务器无响应,qq飞车手游进不去怎么回事 为什么进不去游戏
- where is ConstraintViolationException raised
- [译] 前端组件设计原则
- c++—简单的密码本实现
- 精美的导航引导页html源码
- HR谈网络工程师求职与职业规划
- js- 引用和复制(传值和传址)
- 开源组件 Ehcache中被曝严重漏洞,影响多款Jira产品
- pass 软件_PASS软件非劣效Logrank检验的h1参数如何设置?
- tar.bz2 解压命令
- c语言 dirent,DIR和dirent结构体
- 搭建机器人电控系统——通信协议——IIC通信原理及其实例(库函数+模拟IO口)
- 基于虚拟机的VxWorks实验平台设计与实现(读研时的一篇论文)
- 使用Unity编写传统ARPG游戏的人物操作方式
- 气象ts评分_天气预报评分方法评述.doc
- 【Educoder】Python学习记录(二)
- Redis集群批量删除key
- USB虚拟总线驱动开发扩展之(利用虚拟USB总线驱动实现U盘模拟)