程序设计与算法(三)期末考试(2020春季)

001:编程填空:二进制输出

输入
第一行是整数n(n<15),表示有n个正整数要处理
第二行是n个正整数
输出
对每个给出的正整数,输出其二进制表示。不足31位则用0补齐到31位
样例输入
3
1 2 3
样例输出
0000000000000000000000000000001
0000000000000000000000000000010
0000000000000000000000000000011

#include <iostream>
#include <string>
using namespace std;
string dec2bin(int x) {
	// 在此处补充你的代码
//————————————————————————————————————
	char ch[32];
	for (int i = 30; i >= 0; i--) {
		ch[i] = x % 2 + '0';
		x /= 2;
	}
	ch[31] = 0;
	return ch;
//————————————————————————————————————
}
int main() {
	int n;
	cin >> n;
	while (n--) {
		int x;
		cin >> x;
		cout << dec2bin(x) << endl;
	}
	return 0;
}

002:编程填空:统计动物数量

输入

输出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats

#include <iostream>
using namespace std;
// 在此处补充你的代码
//————————————————————————————
class Animal {
public:
	static int number;
	Animal() { number++; }
	virtual ~Animal() { number--; }
};
class Dog:public Animal {
public:
	static int number;
	Dog() { number++; }
	~Dog() { number--; }
};
class Cat :public Animal {
public:
	static int number;
	Cat() { number++; }
	~Cat() { number--; }
};
int Animal::number = 0, Dog::number = 0, Cat::number = 0;
//————————————————————————————
void print() {
	cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}

int main() {
	print();
	Dog d1, d2;
	Cat c1;
	print();
	Dog* d3 = new Dog();
	Animal* c2 = new Cat;
	Cat* c3 = new Cat;
	print();
	delete c3;
	delete c2;
	delete d3;
	print();
}

003:编程填空:简单的计算

输入
有若干组数据
每组数据三行
第一行是一个浮点数f和一个整数 n
第二行是两个浮点数 x 和 y
第三行是两个整数 p 和q
输出
对每组数据
先输出 x + y - f
再输出 p + q - n
样例输入
2.2 3
1.0 2.0
10 20
4.5 30
4.8 9.2
100 200
样例输出
0.8
27
9.5
270

#include <iostream>
using namespace std;
template <class T>
class Add {
public:
	// 在此处补充你的代码
//—————————————————————————
	T val;
	Add(T x) :val(x) {}
	T operator()(T i, T j) {
		return i + j - val;
	}
//—————————————————————————
};

int main() {
	double f;
	int n;
	while (cin >> f >> n) {

		Add<double> a1(f);
		Add<int> a2(n);
		double x, y;
		int p, q;
		cin >> x >> y >> p >> q;
		cout << a1(x, y) << endl;
		cout << a2(p, q) << endl;
	}
	return 0;
}

004:编程填空:MyClass

样例输入
None
样例输出
A:3
A:15
B:5
3
15
5

#include <iostream>
using namespace std;
class CMyClassA {
	int val;
public:
	CMyClassA(int);
	void virtual print();
};
CMyClassA::CMyClassA(int arg) {
	val = arg;
	printf("A:%d\n", val);
}
void CMyClassA::print() {
	printf("%d\n", val);
	return;
}
// 在此处补充你的代码
//——————————————————————
class CMyClassB:public CMyClassA {
	int val;
public:
	CMyClassB(int arg) :CMyClassA(3 * arg) {
		val = arg;
		printf("B:%d\n", val);
	}
	void print(){
		printf("%d\n", val);
		return;
	}
};
//——————————————————————
int main(int argc, char** argv) {
	CMyClassA a(3), * ptr;
	CMyClassB b(5);
	ptr = &a; ptr->print();
	a = b;
	a.print();
	ptr = &b; ptr->print();
	return 0;
}

005:编程填空:又是MyClass

输入
第一行是整数t表示数据组数
每组数据有两行
第一行开头是整数m,然后后面是m个整数(5 < m < 30)
第二行是一个没有空格的字符串,长度不超过50
输出
对每组数据 先输出m个整数中的第5个,然后输出字符串中的第7个字符。
"第i个"中的 i 是从0开始算的。
样例输入
1
6 1 3 5 5095 8 8
helloworld
样例输出
8 r

#include <iostream>
#include <cstring> 
#include <vector>
#include <cstdio> 
using namespace std;
// 在此处补充你的代码
//——————————————————————————
template<class T>
class CMyClass {
	vector<T>ans;
public:
	CMyClass(T* st, int len) {
		for (int i = 0; i < len; i++)
			ans.push_back(st[i]);
	}
	T operator[](int x) {
		return ans[x];
	}
};
//——————————————————————————
int  a[40];
int main(int argc, char** argv) {
	int t;
	scanf("%d", &t);
	while (t--) {
		int m;
		scanf("%d", &m);
		for (int i = 0; i < m; ++i)
			scanf("%d", a + i);
		char s[100];
		scanf("%s", s);
		CMyClass<int> b(a, m);
		CMyClass<char> c(s, strlen(s));
		printf("%d %c\n", b[5], c[7]);
	}
	return 0;
}

006:编程填空:去除重复元素排序

输入
第一行是个整数,表示输入数据组数
每组数据一行,有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];
		// 在此处补充你的代码
//——————————————————————————————————
		vector<int>b; int c[100];
		sort(a, a + 12); int* end = unique(a, a + 12);
		for (int* it = a; it < end; ++it) {
			cout << *it << ' ';
		}
//——————————————————————————————————
		std::copy(b.begin(), b.end(), c);
		cout << endl;

	}
	return 0;
}

007:编程填空:按要求输出

输入

输出
10 13 18 15 17 12 16 19

#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <stack>
#include <iostream>
#include <set>
using namespace std;
int  a[10] = { 0, 6, 7, 3, 9, 5, 8, 6, 4, 9 };
int  b[10] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };//10 13 18 15 17 12 16 19
int main(int argc, char** argv) {
	// 在此处补充你的代码
//————————————————————
	map<int, int>c;
	map<int, int>::iterator it;
//————————————————————
	for (int i = 0; i < 10; i++)
		c[a[i]] = b[i];
	for (it = c.begin(); it != c.end(); it++)
		cout << it->second << " ";
	return 0;
}

008:编程填空:还是Fun和Do

输入

输出
A::Fun
B::Do
C::Fun
C::Do
A::Fun
B::Do

#include <iostream> 
using namespace std;

class A {
public:
	virtual void Fun() {
		cout << "A::Fun" << endl;
	};
	virtual void Do() {
		cout << "A::Do" << endl;
	}
};
// 在此处补充你的代码
//————————————————————————————————
class B:public A {
public:
	void Do() {
		cout << "B::Do" << endl;
	}
};
class C:public B {
public:
	void Fun() {
		cout << "C::Fun" << endl;
	}
	void Do() {
		cout << "C::Do" << endl;
	}
};
void Call1(A& p)
//————————————————————————————————
{
	p.Fun();
	p.Do();
}
void Call2(B p) {
	p.Fun();
	p.Do();
}



int main() {
	C c;
	B b;
	Call1(b);
	Call1(c);
	Call2(c);
	return 0;
}

009:编程填空:简单的对象

输入

输出
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;
}

010:编程填空:回调函数

输入
多组数据。第一行是数据组数 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
{
	// 在此处补充你的代码
//————————————————————————————————
	int mi;
public:
	MyFunc(int x) :mi(x) {}
	int operator()(int t) {
		int ret = 1;
		for (int i = 0; i < mi; i++)ret *= t;
		return ret;
	}
//————————————————————————————————
};
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;
	}
}

011:编程填空:前K大的偶数

输入
有多组数据
第一行是数据组数 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
{
	// 在此处补充你的代码
//————————————————————————————————————————
	int k;
	priority_queue<int>pq;
public:
	MyQueue(int x) :k(x) {}
	friend istream& operator>>(istream& in, MyQueue& q) {
		int x; in >> x; if (x % 2)return in;
		q.pq.push(x); return in;
	}
	friend ostream& operator<<(ostream& out, MyQueue& q) {
		for (int i = 1; i <= q.k; i++) {
			printf("%d ", q.pq.top());
			q.pq.pop();
		}
		return out;
	}
//————————————————————————————————————————
};
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;
}

012:编程填空:Printer

输入
第一行是整数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

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

using namespace std;


class Printer {
	// 在此处补充你的代码
//————————————————————————————
	int val;
public:
	Printer(int x) :val(x) {}
	void operator()(int x) {
		if (x > val)cout << x << ',';
	}
	void operator()(string s) {
		if (s.length() > val)cout << s << ',';
	}
};
//————————————————————————————
	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;
	}

013:编程填空:三生三世

描述
近年来,国内电视剧吸引了越来越多的关注;有的以当红的演员阵容而吸引观众,比如《三生三世十里桃花》(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* _name, int _actor, int _story, int _acting_skill) {
		strcpy(name, _name); actor = _actor; story = _story; acting_skill = _acting_skill;
	}
	bool operator <(const TV_Drama& x) {
		return actor > x.actor;
	}
};
void Printer(const TV_Drama& x) {
	cout << x.name << ";";
}
bool comparator_1(const TV_Drama& a, const TV_Drama& b) {
	return a.story > b.story;
}
class comparator_2 {
public:
	comparator_2() {}
	bool operator()(const TV_Drama& a, const TV_Drama& b) {
		return a.acting_skill > b.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;
	}

014:编程填空:又见模板

输入
第一行是整数n,表示有n组数据
每组数据有3行
第一行是10个整数
第二行是5个小数
第三行是4个不带空格的字符串,它们之间用空格分隔
输出
先输出10个整数里面的第三个
再输出5个小数的和 (不用考虑小数点后面几位,用cout直接输出即可)
再输出4个字符串连在一起的字符串
样例输入
1
1 2 3 4 5 6 7 8 9 10
4.2 0.0 3.1 2.7 5.2
Hello , world !
样例输出
3
15.2
Hello,world!

#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
template<class T, int size>
class A {
	T a[size];
public:
	A(T* x) {
		for (int i = 0; i < size; i++)
			a[i] = x[i];
	}
	T& operator [](int i) {
		return a[i];
	}
	T sum() {
		T ret = a[0];
		for (int i = 1; i < size; i++)
			ret += a[i];
		return ret;
	}
};
//---------------------------------------
int main() {

	int t;
	cin >> t;
	while (t--) {
		int b1[10];
		for (int i = 0; i < 10; ++i)

			cin >> b1[i];
		A<int, 10> a1 = b1;
		cout << a1[2] << endl;


		double b2[5];
		for (int i = 0; i < 5; ++i)
			cin >> b2[i];

		A<double, 5> a2 = b2;
		cout << a2.sum() << endl;


		string b3[4];
		for (int i = 0; i < 4; ++i)
			cin >> b3[i];

		A<string, 4> a3 = b3;
		cout << a3.sum() << endl;
	}
	return 0;
}

015:编程填空:矩形排序

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

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

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

输入
第一行是一个整数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 {
    int h, w;
    int AllLen, Area;
public:
    Rectangle(int a, int b) :h(a), w(b) {
        AllLen = (a + b) * 2;
        Area = a * b;
    }
    friend bool operator < (const Rectangle& a, const Rectangle& b) {
        if (a.Area == b.Area)return a.AllLen > b.AllLen;
        return a.Area > b.Area;
    }
    friend class Comp;
    friend ostream& operator <<(ostream& os, const Rectangle& x) {
        os << x.Area << " " << x.AllLen; return os;
    }
};
class Comp {
public:
    bool operator()(const Rectangle& a, const Rectangle& b) {
        if (a.AllLen == b.AllLen)return a.Area < b.Area;
        return a.AllLen < b.AllLen;
    }
};
//------------------------------------
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;
}

016:编程填空:维护平面点

描述
程序填空,一开始平面上一个点都没有

每次可以插入一个点,删除一个已经存在的点,或者按照x 或y 来查询一个存在的点

保证任何时候任意两个点一定是一个点严格在另一个点的右下方

即两点(x1, y1), (x2, y2),必定有x1 > x2 且y1 < y2 ,或者x1 < x2 且y1 > y2

输入
输入数据的每一行,格式为以下之一:
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;
// 在此处补充你的代码
class myComp {
public:
	bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
		if (a.first > 0 && a.second > 0 && b.first > 0 && b.second > 0)return a.first > b.first;
		else if (a.first < 0 || b.first < 0) return a.second < b.second;
		else if (a.second < 0 || b.second < 0)return a.first > b.first;
	}
};
//————————————————————————————————————————————————
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
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值