程设期中大复习
1.0403:编程填空:
二进制第n位为1的整数个数
总时间限制: 1000ms 内存限制: 1024kB
描述
给出一组整数,统计其中有多少整数符合条件“二进制表示的第n位为1”
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
int main(int argc, char** argv) {
int n, x;
vector<int> intVec;
cin>>n>>x;
while(x) {
intVec.push_back(x);
cin>>x;
}
cout<<count_if(intVec.begin(), intVec.end(), Pred(n))<<endl;
return 0;
}
输入
输入数据包含两行
第一行为正整数n (0 <= n < 32)
第二行是一组待统计的整数,以0表示结束。
输出
输出一行,为符合条件“二进制表示的第n位为1”的整数个数
样例输入
0
1 2 3 4 5 0
样例输出
3
提示
最右边一位为第0位
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
class Pred{
public:
int n;
Pred(int _n):n(_n){};
bool operator()(int i){
return (i>>n)&1;
}
};
int main(int argc, char** argv) {
int n, x;
vector<int> intVec;
cin>>n>>x;
while(x) {
intVec.push_back(x);
cin>>x;
}
cout<<count_if(intVec.begin(), intVec.end(), Pred(n))<<endl;
return 0;
}
注意:1.count_if的最后一个参数Pred实例化为bool(*Pred)(int i)
2.查询得到accumulate最后一个参数也是如此,参数是*i而不是迭代器
2.0406:编程填空:矩形排序
总时间限制: 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{
public:
int width;
int height;
Rectangle(int w, int h):width(w), height(h){}
bool operator<(Rectangle i)const{
if(width*height == i.width*i.height)
return width+height > i.width+i.height;
else
return width*height > i.width*i.height;
}
friend ostream & operator<<(ostream & o, Rectangle a);
};
struct Comp{
bool operator()(Rectangle a, Rectangle b){
if(a.width+a.height == b.width+b.height)
return a.width*a.height < b.width*b.height;
else
return a.width+a.height < b.width+b.height;
}
};
ostream & operator<<(ostream & o, Rectangle a){
o << a.width*a.height <<' '<< 2*(a.width+a.height);
return o;
}
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;
}
注意:1.multiset实例化时,要求比较函数Comp必须是一个类型,因此函数不可以,要写成class/struct
2.重载<时,必须把函数声明为const型,否则CompileError
3.0408:编程填空:维护平面点
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,一开始平面上一个点都没有
每次可以插入一个点,删除一个已经存在的点,或者按照x 或y 来查询一个存在的点
保证任何时候任意两个点一定是一个点严格在另一个点的右下方
即两点(x1, y1), (x2, y2),必定有x1 > x2 且y1 < y2 ,或者x1 < x2 且y1 > y2
#include <set>
#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
int main() {
string cmd;
set<pair<int, int>, myComp> S;
while (cin >> cmd) {
if (cmd == "A") {
int x, y;
cin >> x >> y;
S.insert(make_pair(x, y));
} else if (cmd == "Qx") {
int x;
cin >> x;
cout << S.lower_bound(make_pair(x, -1))->second << endl;
} else if (cmd == "Qy") {
int y;
cin >> y;
cout << S.lower_bound(make_pair(-1, y))->first << endl;
} else {
int x, y;
cin >> x >> y;
S.erase(make_pair(x, y));
}
}
return 0;
}
输入
输入数据的每一行,格式为以下之一:
A x y
R x y
Qx x
Qy y
其中 x 与 y 都是 0 到 10^9 之间的整数
A x y 表示插入点 (x, y)
R x y 表示删除点 (x, y),保证存在
Qx x 表示在当前所有点中,找到第一维为x的点,输出其第二维的值,保证存在
Qy y 表示在当前所有点中,找到第二维为y的点,输出其第一维的值,保证存在
总共操作数不超过100000
输出
对于每一个 Qx 和 Qy 操作,输出一行表示对应的答案
样例输入
A 3 5
A 4 2
Qx 4
R 4 2
A 4 3
Qy 3
样例输出
2
4
#include <set>
#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
typedef pair<int, int> p;
struct myComp{
bool operator()(p a, p b)const{
if(a.first != -1 && b.first != -1)
return a.first < b.first;
else
return a.second > b.second;
}
};
int main() {
string cmd;
set<pair<int, int>, myComp> S;
while (cin >> cmd) {
if (cmd == "A") {
int x, y;
cin >> x >> y;
S.insert(make_pair(x, y));
} else if (cmd == "Qx") {
int x;
cin >> x;
cout << S.lower_bound(make_pair(x, -1))->second << endl;
} else if (cmd == "Qy") {
int y;
cin >> y;
cout << S.lower_bound(make_pair(-1, y))->first << endl;
} else {
int x, y;
cin >> x >> y;
S.erase(make_pair(x, y));
}
}
return 0;
}
注意:1.使用typedef可以简化程序
2.lower_bound和upper_bound与元素的比较也是利用自己定义的Comp
4.0506:编程填空:去除重复元素排序
总时间限制: 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];
// 在此处补充你的代码
sort(a, a+12);
set<int> b(a, a+12);
ostream_iterator<int> c(cout, " ");
std::copy(b.begin(), b.end(), c);
cout << endl;
}
return 0;
}
注意:1.set用数组初始化之后自然没有重复元素
2.ostream_iterator c(cout, " ");
std::copy(b.begin(), b.end(), c);
奇妙用法
5.0509:编程填空:前K大的偶数
总时间限制: 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 k;
multiset<int, greater<int>> que;
MyQueue(int _k):k(_k){}
friend ostream & operator<<(ostream & o, MyQueue a);
friend istream & operator>>(istream & i, MyQueue & a);
};
istream & operator>>(istream & i, MyQueue & a){
int tmp;
i >> tmp;
if(tmp % 2 == 0)
a.que.insert(tmp);
return i;
}
ostream & operator<<(ostream & o, MyQueue a){
multiset<int, greater<int>>::iterator i = a.que.begin();
int j = 0;
while(i != a.que.end() && j < a.k){
o << *i << ' ';
++i;
++j;
}
return o;
}
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;
}
注意:1.这种有序的题,优先考虑set/map
2.重载>>时,参数a必须是&a,否则赋值无效。返回值必须是istream &。
6.0512:编程填空:简单的对象
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,使得程序输出:
2
1
1
0
#include <iostream>
using namespace std;
class A
{
static int num;
public:
A(){num+=1;}
void func()
{
cout<< num <<endl;
}
// 在此处补充你的代码
};
int A::num=1;
int main()
{
A a1;
const A a2 = a1;
A & a3 = a1;
const A & a4 = a1;
a1.func();
a2.func();
a3.func();
a4.func();
return 0;
}
输入
无
输出
2
1
1
0
#include <iostream>
using namespace std;
class A
{
static int num;
public:
A(){num+=1;}
void func()
{
cout<< num <<endl;
}
// 在此处补充你的代码
void func()const {
num--;
cout << num << endl;
}
};
int A::num=1;
int main()
{
A a1;
const A a2 = a1;
A & a3 = a1;
const A & a4 = a1;
a1.func();
a2.func();
a3.func();
a4.func();
return 0;
}
注意:1.const型对象或对象的引用只能执行const成员函数
7.0513:编程填空:三生三世
总时间限制: 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;
#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 * n, int a, int s, int acsk):actor(a), story(s), acting_skill(acsk){
memset(name, 0, sizeof(name));
int i = 0;
while(n[i]){
name[i] = n[i];
++i;
}
}
bool operator<(TV_Drama a)const{
return actor > a.actor;
}
};
bool comparator_1(TV_Drama a, TV_Drama b){
return a.story > b.story;
}
struct comparator_2{
bool operator()(TV_Drama a, TV_Drama b){
return a.acting_skill > b.acting_skill;
}
};
void Printer(TV_Drama a){
cout << a.name <<';';
}
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;
}
注意;1.Printer没有括号,是函数,且for_each给的参数不是迭代器
2.comparator_1没有括号,是函数;comparator_2()有括号,是类型
3.数组赋值
TV_Drama(char * n, int a, int s, int acsk):actor(a), story(s), acting_skill(acsk){
memset(name, 0, sizeof(name));
int i = 0;
while(n[i]){
name[i] = n[i];
++i;
}
}
8.0811:编程填空:数据库内的学生信息
总时间限制: 3000ms 内存限制: 20480kB
描述
程序填空,使得下面的程序,先输出
(Tom,80),(Tom,70),(Jone,90),(Jack,70),(Alice,100),
(Tom,78),(Tom,78),(Jone,90),(Jack,70),(Alice,100),
(70,Jack),(70,Tom),(80,Tom),(90,Jone),(100,Alice),
(70,Error),(70,Error),(80,Tom),(90,Jone),(100,Alice),
然后,再根据输入数据按要求产生输出数据
#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
struct Student
{
string name;
int score;
};
template <class T>
void Print(T first,T last) {
for(;first!= last; ++ first)
cout << * first << ",";
cout << endl;
}
int main()
{
Student s[] = { {"Tom",80},{"Jack",70},
{"Jone",90},{"Tom",70},{"Alice",100} };
MyMultimap<string,int> mp;
for(int i = 0; i<5; ++ i)
mp.insert(make_pair(s[i].name,s[i].score));
Print(mp.begin(),mp.end()); //按姓名从大到小输出
mp.Set("Tom",78); //把所有名为"Tom"的学生的成绩都设置为78
Print(mp.begin(),mp.end());
MyMultimap<int,string,less<int> > mp2;
for(int i = 0; i<5; ++ i)
mp2.insert(make_pair(s[i].score,s[i].name));
Print(mp2.begin(),mp2.end()); //按成绩从小到大输出
mp2.Set(70,"Error"); //把所有成绩为70的学生,名字都改为"Error"
Print(mp2.begin(),mp2.end());
cout << "******" << endl;
mp.clear();
string name;
string cmd;
int score;
while(cin >> cmd ) {
if( cmd == "A") {
cin >> name >> score;
if(mp.find(name) != mp.end() ) {
cout << "erroe" << endl;
}
mp.insert(make_pair(name,score));
}
else if(cmd == "Q") {
cin >> name;
MyMultimap<string,int>::iterator p = mp.find(name);
if( p!= mp.end()) {
cout << p->second << endl;
}
else {
cout << "Not Found" << endl;
}
}
}
return 0;
}
输入
输入数据的每一行,格式为以下之一:
A name score
Q name score
name是个不带个空格的字符串,长度小于 20
score是个整数,能用int表示
A name score 表示往数据库中新增一个姓名为name的学生,其分数为score。开始时数据库中一个学生也没有。
Q name 表示在数据库中查询姓名为name的学生的分数
数据保证学生不重名。
输入数据少于200,000行。
输出
对于每个查询,输出学生的分数。如果查不到,则输出 “Not Found”
样例输入
A Tom1 30
A Tom2 40
Q Tom3
A Tom4 89
Q Tom1
Q Tom2
样例输出
(Tom,80),(Tom,70),(Jone,90),(Jack,70),(Alice,100),
(Tom,78),(Tom,78),(Jone,90),(Jack,70),(Alice,100),
(70,Jack),(70,Tom),(80,Tom),(90,Jone),(100,Alice),
(70,Error),(70,Error),(80,Tom),(90,Jone),(100,Alice),
Not Found
30
40
提示
-
编写模板的时候,连续的两个 “>”最好要用空格分开,以免被编译器看作是 ">>"运算符。VS可能无此问题,但是Dev C++和服务器上的编译环境会有这个问题。
比如 vector<vector> 有可能出错,要改成 vector<vector > -
在模板中写迭代器时,最好在前面加上 typename关键字,否则可能会编译错。VS可能无此问题,但是Dev C++和服务器上的编译环境会有这个问题。
#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
template<class Key, class Value,class op=greater<Key> > //key从大大小排
template<class T1, class T2 , class Pred = greater<T1> >
class MyMultimap:public multimap<T1, T2, Pred>{
public:
void Set(T1 key, T2 value){
typename multimap<T1, T2, Pred>::iterator start_it, end_it;
start_it = multimap<T1, T2, Pred>::lower_bound(key);
end_it = multimap<T1, T2, Pred>::upper_bound(key);
while (start_it != end_it){
start_it->second = value;
start_it++;
}
}
};
template <class T1, class T2>
ostream& operator << (ostream& o, const pair<T1,T2>& s) {
o << "(" << s.first << "," << s.second << ")";
return o;
}
struct Student
{
string name;
int score;
};
template <class T>
void Print(T first,T last) {
for(;first!= last; ++ first)
cout << * first << ",";
cout << endl;
}
int main()
{
Student s[] = { {"Tom",80},{"Jack",70},
{"Jone",90},{"Tom",70},{"Alice",100} };
MyMultimap<string,int> mp;
for(int i = 0; i<5; ++ i)
mp.insert(make_pair(s[i].name,s[i].score));
Print(mp.begin(),mp.end()); //按姓名从大到小输出
mp.Set("Tom",78); //把所有名为"Tom"的学生的成绩都设置为78
Print(mp.begin(),mp.end());
MyMultimap<int,string,less<int> > mp2;
for(int i = 0; i<5; ++ i)
mp2.insert(make_pair(s[i].score,s[i].name));
Print(mp2.begin(),mp2.end()); //按成绩从小到大输出
mp2.Set(70,"Error"); //把所有成绩为70的学生,名字都改为"Error"
Print(mp2.begin(),mp2.end());
cout << "******" << endl;
mp.clear();
string name;
string cmd;
int score;
while(cin >> cmd ) {
if( cmd == "A") {
cin >> name >> score;
if(mp.find(name) != mp.end() ) {
cout << "erroe" << endl;
}
mp.insert(make_pair(name,score));
}
else if(cmd == "Q") {
cin >> name;
MyMultimap<string,int>::iterator p = mp.find(name);
if( p!= mp.end()) {
cout << p->second << endl;
}
else {
cout << "Not Found" << endl;
}
}
}
return 0;
}
注意:1.可以继承那些写好的类,原有一些写好的函数,只要写上添加的部分即可