程序设计与算法(三)第05周测验(2020春季)

题目链接:http://cxsjsxmooc.openjudge.cn/2020t3spring5/

001:全面的MyString

输入

输出

  1. abcd-efgh-abcd-
  2. abcd-
  3. abcd-efgh-
  4. efgh-
  5. c
  6. abcd-
  7. ijAl-
  8. ijAl-mnop
  9. qrst-abcd-
  10. abcd-qrst-abcd- uvw xyz
    about
    big
    me
    take
    abcd
    qrst-abcd-
    样例输入

    样例输出
  11. abcd-efgh-abcd-
  12. abcd-
  13. abcd-efgh-
  14. efgh-
  15. c
  16. abcd-
  17. ijAl-
  18. ijAl-mnop
  19. qrst-abcd-
  20. abcd-qrst-abcd- uvw xyz
    about
    big
    me
    take
    abcd
    qrst-abcd-

注意出现过的运算符都要重载

代码如下:

#include 
#include 
using namespace std;
int strlen(const char* s)
{
	int i = 0;
	for (; s[i]; ++i);
	return i;
}
void strcpy(char* d, const char* s)
{
	int i = 0;
	for (i = 0; s[i]; ++i)
		d[i] = s[i];
	d[i] = 0;

}
int strcmp(const char* s1, const char* s2)
{
	for (int i = 0; s1[i] && s2[i]; ++i) {
		if (s1[i] < s2[i])
			return -1;
		else if (s1[i] > s2[i])
			return 1;
	}
	return 0;
}
void strcat(char* d, const char* s)
{
	int len = strlen(d);
	strcpy(d + len, s);
}
class MyString
{
	// 在此处补充你的代码
//-------------------------分-------------割-------------线--------------------------
	char* p;
public:
	MyString() { p = NULL; }
	MyString(const char* s) {
		p = new char[strlen(s) + 1];
		strcpy(p, s);
	}
	MyString(const MyString& B) {
		p = new char[strlen(B.p) + 1];
		strcpy(p, B.p);
	}
	~MyString() {
		delete[]p;
	}
	char& operator [] (int pos) {
		return p[pos];
	}
	friend MyString operator+(const char* s, const MyString& B)
	{
		MyString ret(s);
		ret = ret + B;
		return ret;
	}
	MyString operator+(const MyString& B)
	{
		MyString ret;
		ret.p = new char[strlen(p) + strlen(B.p) + 1];
		strcpy(ret.p, p);
		strcat(ret.p, B.p);
		return ret;
	}
	MyString operator+(const char* B)
	{
		MyString ret;
		MyString r(B);
		ret = *this + r;
		return ret;
	}
	MyString& operator +=(const char* B) {
		MyString ret;
		MyString r(B);
		ret = *this + r;
		*this = ret;
		return *this;
	}
	MyString& operator = (const MyString& A) {
		if (A.p) {
			if (p)delete[]p;
			p = new char[strlen(A.p) + 1];
			strcpy(p, A.p);
		}
	}
	MyString operator()(int st, int len) {
		MyString ret; delete ret.p;
		ret.p = new char[len + 1];
		for (int i = 0; i < len; i++)
			ret.p[i] = p[st + i];
		ret.p[len] = '\0';
		return ret;
	}
	bool operator < (const MyString& B) {
		return strcmp(p, B.p) < 0;
	}
	bool operator >(const MyString& B) {
		return strcmp(p, B.p) > 0;
	}
	bool operator == (const MyString& B) {
		return strcmp(p, B.p) == 0;
	}
	friend ostream& operator<<(ostream& out, const MyString& A) {
		if (A.p)out << A.p;
		return out;
	}
//-------------------------分-------------割-------------线--------------------------
};


int CompareString(const void* e1, const void* e2)
{
	MyString* s1 = (MyString*)e1;
	MyString* s2 = (MyString*)e2;
	if (*s1 < *s2)
		return -1;
	else if (*s1 == *s2)
		return 0;
	else if (*s1 > * s2)
		return 1;
}
int main()
{
	MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);
	MyString SArray[4] = { "big","me","about","take" };
	cout << "1. " << s1 << s2 << s3 << s4 << endl;
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A';
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	qsort(SArray, 4, sizeof(MyString), CompareString);
	for (int i = 0; i < 4; i++)
		cout << SArray[i] << endl;
	//s1的从下标0开始长度为4的子串
	cout << s1(0, 4) << endl;
	//s1的从下标5开始长度为10的子串
	cout << s1(5, 10) << endl;
	return 0;
}

002:继承自string的MyString

代码如下:

#include 
#include 
#include 
#include 
using namespace std;
class MyString :public string
{
	// 在此处补充你的代码
//-------------------------分-------------割-------------线--------------------------
public://MyString就是string
	MyString() :string(){}
	MyString(const char* s) :string(s) {}
	MyString(const string& s) :string(s) {}
	MyString(const MyString& A) :string(A){}
	MyString operator()(int st, int len) {
		return substr(st, len);
	}
//-------------------------分-------------割-------------线--------------------------
};


int main()
{
	MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);
	MyString SArray[4] = { "big","me","about","take" };
	cout << "1. " << s1 << s2 << s3 << s4 << endl;
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A';
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	sort(SArray, SArray + 4);
	for (int i = 0; i < 4; i++)
		cout << SArray[i] << endl;
	//s1的从下标0开始长度为4的子串
	cout << s1(0, 4) << endl;
	//s1的从下标5开始长度为10的子串
	cout << s1(5, 10) << endl;
	return 0;
}

003:魔兽世界之二:装备

总时间限制: 1000ms 内存限制: 65536kB
描述
魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。

红司令部,City 1,City 2,……,City n,蓝司令部

两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值这两种属性。
有的武士可以拥有武器。武器有三种,sword, bomb,和arrow,编号分别为0,1,2。
双方的武士编号都是从1开始计算。红方制造出来的第 n 个武士,编号就是n。同样,蓝方制造出来的第 n 个武士,编号也是n。

不同的武士有不同的特点。
dragon 可以拥有一件武器。编号为n的dragon降生时即获得编号为 n%3 的武器。dragon还有“士气”这个属性,是个浮点数,其值为它降生后其司令部剩余生命元的数量除以造dragon所需的生命元数量。
ninja可以拥有两件武器。编号为n的ninja降生时即获得编号为 n%3 和 (n+1)%3的武器。
iceman有一件武器。编号为n的iceman降生时即获得编号为 n%3 的武器。
lion 有“忠诚度”这个属性,其值等于它降生后其司令部剩余生命元的数目。
wolf没特点。
请注意,在以后的题目里,武士的士气,生命值,忠诚度在其生存期间都可能发生变化,都有作用,武士手中的武器随着使用攻击力也会发生变化。

武士在刚降生的时候有一个生命值。

在每个整点,双方的司令部中各有一个武士降生。

红方司令部按照 iceman、lion、wolf、ninja、dragon 的顺序循环制造武士。

蓝方司令部按照 lion、dragon、ninja、iceman、wolf 的顺序循环制造武士。

制造武士需要生命元。

制造一个初始生命值为 m 的武士,司令部中的生命元就要减少 m 个。

如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。
给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。
一共有两种事件,其对应的输出样例如下:

  1. 武士降生
    输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter
    表示在 4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。
    如果造出的是dragon,那么还要输出一行,例:
    It has a arrow,and it’s morale is 23.34
    表示该dragon降生时得到了arrow,其士气是23.34(为简单起见,本题中arrow前面的冠词用a,不用an,士气精确到小数点后面2位,四舍五入)
    如果造出的是ninja,那么还要输出一行,例:
    It has a bomb and a arrow
    表示该ninja降生时得到了bomb和arrow。
    如果造出的是iceman,那么还要输出一行,例:
    It has a sword
    表示该iceman降生时得到了sword。
    如果造出的是lion,那么还要输出一行,例:
    It’s loyalty is 24
    表示该lion降生时的忠诚度是24。
  2. 司令部停止制造武士
    输出样例: 010 red headquarter stops making warriors
    表示在 10点整,红方司令部停止制造武士

输出事件时:

首先按时间顺序输出;

同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。

输入
第一行是一个整数,代表测试数据组数。

每组测试数据共两行。

第一行,一个整数M。其含义为: 每个司令部一开始都有M个生命元( 1 <= M <= 10000)

第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000
输出
对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。
对每组测试数据,首先输出“Case:n" n是测试数据的编号,从1开始
接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。
样例输入
1
20
3 4 5 6 7
样例输出
Case:1
000 red iceman 1 born with strength 5,1 iceman in red headquarter
It has a bomb
000 blue lion 1 born with strength 6,1 lion in blue headquarter
It’s loyalty is 14
001 red lion 2 born with strength 6,1 lion in red headquarter
It’s loyalty is 9
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
It has a arrow,and it’s morale is 3.67
002 red wolf 3 born with strength 7,1 wolf in red headquarter
002 blue ninja 3 born with strength 4,1 ninja in blue headquarter
It has a sword and a bomb
003 red headquarter stops making warriors
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
It has a bomb
004 blue headquarter stops making warriors

#include
#include
#include
using namespace std;
const string Weapon[3] = { "sword","bomb","arrow" };
const string Name[5] = { "dragon" ,"ninja","iceman","lion","wolf" };
const int r_make[5] = { 2,3,4,1,0 };
const int b_make[5] = { 3,0,1,2,4 };
int _time, T, M, cost[5], r_number[5], b_number[5];
class Warrior {
	int num, hp, name_num;
public:
	Warrior(int _num, int _cost, int id_num) :num(_num), hp(_cost), name_num(id_num) {}
};
class dragon :public Warrior {
public:
	int weapon_num; double morale;
	dragon(int _num, double left) :weapon_num(_num % 3), morale(left / cost[0]), Warrior(_num, cost[0], 0) {}
};
class ninja :public Warrior {
public:
	int weapon_num_1, weapon_num_2;
	ninja(int _num) :weapon_num_1(_num % 3), weapon_num_2((_num + 1) % 3), Warrior(_num, cost[1], 1) {}
};
class iceman :public Warrior {
public:
	int weapon_num;
	iceman(int _num) :weapon_num(_num % 3), Warrior(_num, cost[2], 2) {}
};
class lion :public Warrior {
public:
	int loyalty;
	lion(int _num, int LOY) :loyalty(LOY), Warrior(_num, cost[3], 3) {}
};
class wolf :public Warrior {
public:
	wolf(int _num) :Warrior(_num, cost[4], 4) {}
};
class headquarter {
public:
	int Yuan, NowPos;
	bool Over;
	headquarter(int m, int pos = 0, bool ov = false) :Yuan(m), NowPos(pos), Over(ov) {}
};
int main(void) {
	scanf("%d", &T);
	for(int kase=1;kase<=T;kase++){
		scanf("%d", &M);
		_time = 0;
		memset(r_number, 0, sizeof(r_number));
		memset(b_number, 0, sizeof(b_number));
		for (int i = 0; i < 5; i++)
			scanf("%d", &cost[i]);
		printf("Case:%d\n", kase);
		headquarter red(M), blue(M);
		int flag1 = 1, flag2 = 1;
		while (1) {
			if (!flag1 && !flag2)break;
			if (!red.Over) {
				int cnt = 0;
				while (red.Yuan < cost[r_make[red.NowPos]]) {
					red.NowPos = (red.NowPos + 1) % 5;
					cnt++; if (cnt == 5)break;
				}
				if (cnt == 5) {
					red.Over = true;
					printf("%03d red headquarter stops making warriors\n", _time); flag1 = 0;
				}
				else {
					r_number[r_make[red.NowPos]]++;
					printf("%03d red %s %d born with strength %d,%d %s in red headquarter\n", _time, Name[r_make[red.NowPos]].c_str(), _time + 1, cost[r_make[red.NowPos]], r_number[r_make[red.NowPos]], Name[r_make[red.NowPos]].c_str());
					switch (r_make[red.NowPos]) {
					case 0: {
						red.Yuan -= cost[0];
						dragon a(_time + 1, red.Yuan);
						printf("It has a %s,and it's morale is %.2lf\n", Weapon[a.weapon_num].c_str(), a.morale);
						break;
					}
					case 1: {
						red.Yuan -= cost[1];
						ninja a(_time + 1);
						printf("It has a %s and a %s\n", Weapon[a.weapon_num_1].c_str(), Weapon[a.weapon_num_2].c_str());
						break;
					}
					case 2: {
						red.Yuan -= cost[2];
						iceman a(_time + 1);
						printf("It has a %s\n", Weapon[a.weapon_num].c_str());
						break;
					}
					case 3: {
						red.Yuan -= cost[3];
						lion a(_time + 1, red.Yuan);
						printf("It's loyalty is %d\n", a.loyalty);
						break;
					}
					default:
						red.Yuan -= cost[4];
						break;
					}
					red.NowPos = (red.NowPos + 1) % 5;
				}
			}
			if (!blue.Over) {
				int cnt = 0;
				while (blue.Yuan < cost[b_make[blue.NowPos]]) {
					blue.NowPos = (blue.NowPos + 1) % 5;
					cnt++; if (cnt == 5)break;
				}
				if (cnt == 5) {
					blue.Over = true;
					printf("%03d blue headquarter stops making warriors\n", _time); flag2 = 0;
				}
				else {
					b_number[b_make[blue.NowPos]]++;
					printf("%03d blue %s %d born with strength %d,%d %s in blue headquarter\n", _time, Name[b_make[blue.NowPos]].c_str(), _time + 1, cost[b_make[blue.NowPos]], b_number[b_make[blue.NowPos]], Name[b_make[blue.NowPos]].c_str());
					switch (b_make[blue.NowPos]) {
					case 0: {blue.Yuan -= cost[0];
						dragon a(_time + 1, blue.Yuan);
						printf("It has a %s,and it's morale is %.2lf\n", Weapon[a.weapon_num].c_str(), a.morale);
						break;
					}
					case 1: {
						blue.Yuan -= cost[1];
						ninja a(_time + 1);
						printf("It has a %s and a %s\n", Weapon[a.weapon_num_1].c_str(), Weapon[a.weapon_num_2].c_str());
						break;
					}
					case 2: {
						blue.Yuan -= cost[2];
						iceman a(_time + 1);
						printf("It has a %s\n", Weapon[a.weapon_num].c_str());
						break;
					}
					case 3: {
						blue.Yuan -= cost[3];
						lion a(_time + 1, blue.Yuan);
						printf("It's loyalty is %d\n", a.loyalty);
						break;
					}
					default:
						blue.Yuan -= cost[4];
						break;
					}
					blue.NowPos = (blue.NowPos + 1) % 5;
				}
			}
			_time++;
		}
	}
	return 0;
}

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

代码如下:

#include 
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;//初始化不能a=b=c=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();
}

选择题

程序设计与算法(三)第05周测验(2020春季)_第1张图片
程序设计与算法(三)第05周测验(2020春季)_第2张图片
程序设计与算法(三)第05周测验(2020春季)_第3张图片
程序设计与算法(三)第05周测验(2020春季)_第4张图片
程序设计与算法(三)第05周测验(2020春季)_第5张图片

你可能感兴趣的:(面向对象(C++))