华南师大19级计算机学院专硕上机题

一、 编程计算1/1!-1/3!+1/5!-…+(-1)(n+1)/(2n-1)!

#include <iostream>
using namespace std;

int main() {

	int n;				//输入n
	cin >> n;

	int t = 1;		
	double num = 1;			//定义变量
	double sum = 0;
	double temp = num;

	while (n>0) {
		sum += 1 / temp * t;
		t = -t;
		num++;
		temp *= num;
		num++;
		temp *= num;
		n--;
	}
	cout << sum;
	return 0;
}

二、 甲乙丙对一次竞赛名次进行预测。

甲:A第1 B第3

乙:C第1 D第4

丙:D第1 B第3 (数据不是原题,记不太清,但解题方法是一样的)

他们都说对了一半,求ABCD正确的名次(ABCD不会出现相同的名次)

#include <iostream>
using namespace std;

int main() {
	int A, B, C, D;
	for (A = 1; A <= 4; A++) {
		for (B = 1; B <= 4; B++) {
			if ((A == 1 && B != 3) || (A != 1 && B == 3)&&A!=B) {
				for (C = 1; C <= 4; C++) {
					if (A != C && B != C) {
						for (D = 1; D <= 4; D++) {
							if (D!=A&&D!=B&&D!=C&&((C == 1 && D != 4) || (C != 1 && D == 4))
								&& ((D == 1 && B != 3) || (D != 1 && B == 3))) {
								cout << "A:" << A << endl;
								cout << "B:" << B << endl;
								cout << "C:" << C << endl;
								cout << "D:" << D << endl;
								cout << endl;
							}
						}
					}
				}
			}
			
		}
	}
	return 0;
}

三、 给定链表节点的定义

struct Node{

int data;

Node *next;

}

请编写一个函数,用递归的方式:对两个有序链表合并成一个有序链表。

#include <iostream>
using namespace std;

struct Node {		//结点定义
	int data;
	Node* next;
};

/**
	合并两个链表到新的链表方法
*/
void merge(Node* newList,Node* list1, Node* list2) {
	//如果两个链表都非空
	if (list1 != NULL && list2 != NULL) {
		if (list1->data <= list2->data) {		//如果链表1<=链表2
			newList->next = list1;				//新链表的下一个节点指向链表1
			newList = newList->next;			//新链表切换下一个节点
			merge(newList, list1->next, list2);	//递归合并
		}
		else {									//如果链表1>链表2
			newList->next = list2;				//新链表的下一个节点指向链表2
			newList = newList->next;			//新链表切换下一个节点
			merge(newList, list1, list2->next);	//递归合并
		}
	}

	if (list1!= NULL&&list2==NULL) {		//第二种情况,list1非空,list2空
		newList->next = list1;
		newList = newList->next;
		merge(newList, list1->next, NULL);
	}
	if (list2 != NULL&&list1==NULL) {		//第三种情况,list1空,list2非空
		newList->next = list2;
		newList = newList->next;
		merge(newList, NULL, list2->next);
	}
}

int main() {
	Node* newList = new Node{ NULL, NULL };		//创建新链表

	Node* list1 = new Node{ NULL,NULL };		//创建链表1
	Node* LinkList1 = list1;					//保存链表1头结点
	cout << "请依次输入链表1的值,当输入-1时结束:";
	int data;
	cin >> data;
	while (data != -1) {						//创建链表1
		Node* newNode = new Node{ data,NULL };
		list1->next = newNode;
		list1 = list1->next;
		cin >> data;
	}

	Node* list2 = new Node{ NULL,NULL };		//创建链表2
	Node* LinkList2 = list2;					//保存链表2头结点
	cout << "请依次输入链表2的值,当输入-1时结束:";
	int data2;
	cin >> data2;
	while (data2 != -1) {						//创建链表2
		Node* newNode = new Node{ data2,NULL };
		list2->next = newNode;
		list2 = list2->next;
		cin >> data2;
	}

	merge(newList, LinkList1->next, LinkList2->next);	//调用递归算法

	Node* p = newList->next;		//遍历输出结果
	for (;p!=NULL;p=p->next) {
		cout << p->data << " ";
	}
}
请依次输入链表1的值,当输入-1时结束:1 3 4 5 8 -1
请依次输入链表2的值,当输入-1时结束:3 6 9 10 11 14 -1
合成后的链表:1 3 3 4 5 6 8 9 10 11 14

四、 现有一个酒店场景。定义一个客人类Guest。包含成员属性:编号Num、姓名Name、房费Fee、当前酒店入住人数Count。其中编号Num需要程序自动生成。现在要求实现以下Guest的成员函数:构造函数、Show()显示Guest的信息、GetCount()返回当前酒店入住的人数、GetTotalIncome()返回当前酒店的总收入。并定义3个Guest对象来对成员函数进行测试。

#include <iostream>
#include <iostream>
using namespace std;

static int Total = 0;		//定义订单编号,从0开始不会出现重复

class Guest {
private:
	int Num;
	string Name;
	double Fee;
	int Count;
public:
	Guest();
	Guest(string n, double f, int c);
	void Show();
	int getCount();
	static double getTotalIncome(Guest* guest,int n);	//返回总收入
};

Guest::Guest() {}
Guest::Guest(string n, double f, int c) {
	Num = Total++;
	Name = n;
	Fee = f;
	Count = c;
}

void Guest::Show() {
	cout << "Num:" << Num << endl;
	cout << "Name:" << Name << endl;
	cout << "Fee:" << Fee << endl;
	cout << "Count:" << getCount() << endl;
	cout << endl;
}

int Guest::getCount() {
	return Count;
}

double Guest::getTotalIncome(Guest *guest,int n) {
	double sum = 0;
	int i = 0;
	for(int i=0;i<n;i++) {
		sum += (guest+i)->Fee;
	}
	return sum;
}

int main() {
	Guest guests[3] = {
		Guest("张三",999,3),
		Guest("李四",200,1),
		Guest("王五",1200,4)
	};
	for (int i = 0; i < 3; i++) {
		guests[i].Show();
	}
	double totalIncome = Guest::getTotalIncome(guests,3);
	cout << "totalIncome:" << totalIncome;
	return 0;
}

五、现有一抽象类Shape,它拥有一系列虚函数:Input()输入类需要的信息、Show()显示类的信息、Perimeter()计算周长、Area()计算面积。先定义Circle、Square、Triangle来继承Shape并实现其虚函数。要求创建Circle、Square、Triangle的对象,用基类指针指向这些对象,并调用成员函数进行测试。

#include <iostream>
#include <cmath>
using namespace std;

class Shape {
public:
	virtual void Input()=0;			//注意抽象类中方法的写法
	virtual void Show()=0;
	virtual double Perimeter()=0;
	virtual double Area()=0;
};

class Circle :public Shape {
private:
	double radius;
public:
	Circle() {}
	void Input() {
		cout << "请输入圆半径radius:";
		cin >> radius;
	}
	void Show() {
		cout << "radius:" << radius << endl;
		cout << "perimeter" << Perimeter() << endl;
		cout << "Area" << Area() << endl;
		cout << endl;
	}
	double Perimeter() {
		return 3.14 * 2 * radius;
	}
	double Area() {
		return 3.14 * radius * radius;
	}
};

class Square :public Shape {
private:
	double len;
public:
	Square() {}
	void Input() {
		cout << "请输入正方形边长len:";
		cin >> len;
	}
	void Show() {
		cout << "len:" << len << endl;
		cout << "perimeter:" << Perimeter() << endl;
		cout << "area:" << Area() << endl;
		cout << endl;
	}

	double Perimeter() {
		return 4 * len;
	}
	double Area() {
		return len * len;
	}
};

class Triangle :public Shape {
private:
	double a, b, c;
public:
	Triangle() {}
	void Input() {
		cout << "请输入三角形边长a,b,c:";
		cin >> a >> b >> c;
	}
	void Show() {
		cout << "a:" << a << endl;
		cout << "b:" << b << endl;
		cout << "c:" << c << endl;
		cout<<"perimeter:"<<Perimeter()<<endl;
		cout<<"area:"<<Area();
		cout << endl;
	}

	double Perimeter() {
		return a + b + c;
	}
	double Area() {
		double s = (a + b + c) / 2;
		return sqrt(s * (s - a) * (s - b) * (s - c));
	}
};



int main() {
	Shape* p;
	p = new Circle;
	p->Input();
	p->Show();

	p = new Square;
	p->Input();
	p->Show();

	p = new Triangle;
	p->Input();
	p->Show();

	return 0;
}
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值