2020秋資料結構實驗第一題:兔子數列模擬(斐波那契數列模擬)C++實現

USTC暖暖發表於2020-09-24

實驗題目描述:

  1. 程式設計實現兔⼦問題,歷史上有⼀個有名的關於兔⼦的問題:假設有⼀對兔⼦,⻓兩個⽉它們就算 ⻓⼤成年了。然後以後每個⽉都會⽣出1對兔⼦,⽣下來的兔⼦也都是⻓兩個⽉就算成年,然後每 個⽉也都會⽣出1對兔⼦了。這⾥假設兔⼦不會死,每次都是隻⽣1對兔⼦,實現計算第20個(第 15)⽉的兔⼦對數
  2. 加入條件,每對兔⼦⽣完2次(N次)兔⼦後會死亡,實現計算第20個(第15)⽉的兔⼦對數
  3. 每隔⼏個⽉可以繁殖設定為引數K, 繁殖⼏代後死亡設定成引數P,計算第N個⽉的兔⼦對數

題目分析:

看似題目是三個逐步遞進的過程,其實還是比較簡單的,只需要建立一個簡單的結構體或者類,,然後根據相應的標誌位設定是否死亡,是否長大,是否新生,是否可繁殖這四步,主要的其實是兩個動作,一個出生,一個死亡,就是結構體陣列的刪除和新建罷了,後面放出原始碼,未加入繁殖兩代之間的間隔,可自行加入,大家僅供參考,不可原版照抄!

題目實現原始碼

#include<iostream>
#include<stdio.h>
#include<vector>
#include<String>

using namespace std;

class rabbit {
private:
	bool Is_Adult;
	bool Is_Death;
	int Grown_Time;
	int Breed_Time;
	int DEATH_TIME;
	int MATURE_TIME;
public:
	rabbit(int Death_Time, int Mature_Time) {
		DEATH_TIME = Death_Time;
		MATURE_TIME = Mature_Time;
		Is_Adult = false;
		Is_Death = false;
		Grown_Time = 0;
		Breed_Time = 0;

	}
	bool Get_Is_Adult() {
		return Is_Adult;
	}
	bool Get_Is_Death() {
		return Is_Death;
	}
	int Get_Grown_Time() {
		return Grown_Time;
	}
	int Get_Breed_Time() {
		return Breed_Time;
	}
	void Add_Grown_Time() {
		Grown_Time++;
		if (Grown_Time >= MATURE_TIME)
			Is_Adult = true;
	}
	void Add_Breed_Time() {
		Breed_Time++;
		if (Breed_Time >= DEATH_TIME)
			Is_Death = true;
	}

};
int Compute_Num(int Month_Num, vector<rabbit>Rabbits, int Death_Time, int Mature_Time) {
	for (int i = 0; i < Month_Num; i++)
	{
		int Rabbits_Size_Now = Rabbits.size();
		int Dead_Rabbits = 0;
		int Born_Rabbits = 0;
		for (int j = 0; j < Rabbits_Size_Now;)
		{
			if (Rabbits[j].Get_Is_Death())
			{
				Rabbits.erase(Rabbits.begin() + j);
				Rabbits_Size_Now--;
				Dead_Rabbits++;
				continue;
			}
			if (Rabbits[j].Get_Is_Adult()) {
				rabbit *r = new rabbit(Death_Time, Mature_Time);
				Rabbits.push_back(*r);
				Rabbits[j].Add_Breed_Time();
				Born_Rabbits++;
				Rabbits_Size_Now++;
				if (Rabbits[j].Get_Is_Death())
				{
					Rabbits.erase(Rabbits.begin() + j);
					Rabbits_Size_Now--;
					Dead_Rabbits++;
					continue;
				}
			}
			else
				Rabbits[j].Add_Grown_Time();
			j++;
		}
		printf("The %2d month has %2d rabbits!\n",i+1,Rabbits.size());
		if (Dead_Rabbits)
			printf("The %2d month has %2d rabbits dead!\n", i+1,Dead_Rabbits);
		if (Born_Rabbits)
			printf("The %2d month has %2d rabbits born!\n", i+1, Born_Rabbits);
		//cout << "The " << i + 1 << " month has " << Rabbits.size() << " rabbits!" << endl;
	}
	return Rabbits.size();
}
int main() {
	int Death_Time, Mature_Time, Month_Num;
	char s[200] = "Example: Death_Time = 100000000, Mature_Time = 2, Month_Num = 12";
	cout << "/";
	for (int i = 0; i < strlen(s); i++)
		cout << "*";
	cout << "/"<<endl;
	cout << s << endl;
	cout << "/";
	for (int i = 0; i < strlen(s); i++)
		cout << "*";
	cout << "/";
	cout << endl;
	char s1[100] = "You will get a fibonacci sequence!";
	cout << s1 << endl;
	cout << "/";
	for (int i = 0; i < strlen(s); i++)
		cout << "*";
	cout << "/";
	cout << endl;
	while (1) {
			cout << "Please input Death_Time: ";
			cin >> Death_Time;
			while (cin.fail()) {
				cout << "Wrong, you have inputed a wrong type data!" << endl;
				cin.clear();//清除錯誤標記,重新開啟輸入流,但是輸入流中依舊保留著之前的不匹配的型別
				/*cin.sync();*///清楚cin快取區的資料。
				while (cin.get() != '\n') {
					continue;
				}
				cout << "please input Death_Time again" << endl;
				cin >> Death_Time;
			}
			cout << "Please input Mature_Time: ";
			cin >> Mature_Time;
			while (cin.fail()) {
				cout << "Wrong, you have inputed a wrong type data!" << endl;
				cin.clear();//清除錯誤標記,重新開啟輸入流,但是輸入流中依舊保留著之前的不匹配的型別
				/*cin.sync();*///清楚cin快取區的資料。
				while (cin.get() != '\n') {
					continue;
				}
				cout << "please input Mature_Time again" << endl;
				cin >> Mature_Time;
			}
			cout << "Please input Month_Num to Compute: ";
			cin >> Month_Num;
			while (cin.fail()) {
				cout << "Wrong, you have inputed a wrong type data!" << endl;
				cin.clear();//清除錯誤標記,重新開啟輸入流,但是輸入流中依舊保留著之前的不匹配的型別
				/*cin.sync();*///清楚cin快取區的資料。
				while (cin.get() != '\n') {
					continue;
				}
				cout << "please input Month_Num again" << endl;
				cin >> Month_Num;
			}

		vector<rabbit>Test;
		rabbit* a = new rabbit(Death_Time, Mature_Time);
		Test.push_back(*a);
		int rabbit_sum_num = Compute_Num(Month_Num, Test, Death_Time, Mature_Time);
	}
}

 

相關文章