1. 程式人生 > 其它 >華南師大19級計算機學院專碩上機題

華南師大19級計算機學院專碩上機題

技術標籤:華師專碩上機考研華南師大c++

一、 程式設計計算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;
}